title: Building a Chat Application using Ruby
body: <p>I am trying to build a chat application purely using Ruby. There is a <a href="https://stackoverflow.com/questions/1838149/how-to-build-a-web-based-chat-system-using-ruby-gserver">similar question</a> posted earlier, but I have different and related queries. I have looked at <a href="http://www.rubyinside.com/advent2006/10-gserver.html" rel="nofollow noreferrer">this example</a>(Same as referred by the person who posted a similar question earlier) . The code in the example doesn't seem to be working for me. On running the ruby script on the terminal, and connecting to the url: <a href="http://localhost:1234" rel="nofollow noreferrer">http://localhost:1234</a> in my browser, I indefinitely encounter a "Transferring data from localhost..." message. </p>
  
  <p>Here 1234 is the port number used in the example provided. I am not able to figure out what is the reason behind my unsuccessful run. May be I need to specify something in the command line while executing the script or I am supposed to start the chat(input output) through some other place(probably the browser). I am not able to figure out what exactly to do. Could you please help me out on this?</p>
  
  <p>I am running the chat server code pretty much unmodified. I am running the web service and the chat server on the same host.</p>
  
  <p>I was able to partially get the code working for me upto the point where the loop starts. The modified code which worked for me upto a certain point is given below.</p>
  
  <pre><code>require 'gserver'
  
  class BasicServer &lt; GServer
  
    def initialize(*args)
      super(*args)
  
      # Keep an overall record of the client IDs allocated
      # and the lines of chat
      @@client_id = 0
      @@chat = []
    end
  
  
    def serve(io)
    #  io.puts("Hello world!")
        # Increment the client ID so each client gets a unique ID
      @@client_id += 1
      my_client_id = @@client_id
      my_position = @@chat.size
  
  
     # io.puts(@@chat.size)    
      # Give the total number of people who are currently on chat.. for e.g. 0 =&gt; 1 person on chat    
  
      # Leave a message on the chat queue to signify this client
      # has joined the chat
      @@chat &lt;&lt; [my_client_id, ""]
  
     # io.puts(@@chat)    
  
    end
  
  end
  
  
  server = BasicServer.new(1234)
  server.start
  
  #sleep 120
  #server.shutdown
  </code></pre>
  
  <p>For every browser instance a new client is connected to the chat queue(they have unique client id's to identify them). I wanted to reuse the code in the example by adding a text box(something similar to what we use in html) to the browser instance(s) running wherein a user(s) can enter their message and post it say using the click of a button(which is also integrated in the browser). This reflects in all other browser instances of various clients and the chat goes on like this until users enter a string "quit" to leave the chat room. </p>
  
  <p>I am not sure how to implement the above feature too in Ruby, any suggestions or related articles to refer would be really helpful.</p>
  
  <p>Thanks a lot!!</p>