boddhisattva
boddhisattva

Reputation: 7380

Building a Chat Application using Ruby

I am trying to build a chat application purely using Ruby. There is a similar question posted earlier, but I have different and related queries. I have looked at this example (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: http://localhost:1234 in my browser, I indefinitely encounter a "Transferring data from localhost..." message.

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.

I am running the chat server code pretty much unmodified. I am running the web service and the chat server on the same host.

I was able to partially get the code working for me up to the point where the loop starts. The modified code which worked for me up to a certain point is given below.

require 'gserver'

class BasicServer < 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 => 1 person on chat    

    # Leave a message on the chat queue to signify this client
    # has joined the chat
    @@chat << [my_client_id, ""]

   # io.puts(@@chat)    

  end

end


server = BasicServer.new(1234)
server.start

#sleep 120
#server.shutdown

For every browser instance a new client is connected to the chat queue (they have unique client IDs 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.

I am not sure how to implement the above feature too in Ruby.

Upvotes: 6

Views: 2817

Answers (1)

sarahhodne
sarahhodne

Reputation: 10106

I'm guessing that this is the only code you're using. The way HTTP (the protocol your web browser uses when talking to your server) works, is that the browser connects to your servers, sends some lines saying which page it wants to get, what cookies it has, etc. The server then responds, and in most cases, closes the connection. So in your case, when you connect to the chat server in your web browser, the browser connects, BasicServer#serve gets called, some stuff is sent back, and the web browser closes the connection, so the server can't send more data to the browser. The easiest way to "fix" this is to use a different way of connecting to your server (either telnet or nc (netcat)). If you want this to be in a browser, you need to make your server respond to HTTP requests, and then return a page that regularly polls the server (read up on AJAX or WebSockets). You can look at this for inspiration, although it isn't written in Ruby: https://github.com/ry/node_chat.

Upvotes: 4

Related Questions