Building a Chat application in Ruby

Hi,

I am trying to build a chat application purely using Ruby. I have looked
at the example on Build a Chat Server in Minutes with Ruby and GServer .
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. Could you please help me out on this?

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 upto the point where
the loop starts. The modified code which worked for me upto 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

Hope you could give me some insights on what could be going wrong from
my end.

Thanks a lot for your time!!

On Thursday, December 30, 2010 11:12:52 pm Mohnish J. wrote:

Hi,

I am trying to build a chat application purely using Ruby. I have looked
at the example on Build a Chat Server in Minutes with Ruby and GServer .
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.

GServer is a TCP server. It’s not a webserver. You can certainly build a
webserver on top of it, but I don’t know why you would, when there’s
five or
ten good webservers for Ruby already, available as gems.

From your code, it sure doesn’t look like you’ve written a webserver, so
maybe
open a terminal and type:

nc localhost 1234

…except that’s probably going to just quit, since you don’t actually
ever
write anything, as far as I can tell.

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).

Since it’s a GServer example, I’m guessing you want something like
netcat.

Also, it looks like the “serve” method is designed to be an entire TCP
connection, so if you were intending this to be a persistent connection
(as
you’d have with a chat server), you’d need some sort of loop, as in the
original code.

David M. wrote in post #971588:

On Thursday, December 30, 2010 11:12:52 pm Mohnish J. wrote:

Hi,

I am trying to build a chat application purely using Ruby. I have looked
at the example on Build a Chat Server in Minutes with Ruby and GServer .
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.

GServer is a TCP server. It’s not a webserver. You can certainly build a
webserver on top of it, but I don’t know why you would, when there’s
five or
ten good webservers for Ruby already, available as gems.

From your code, it sure doesn’t look like you’ve written a webserver, so
maybe
open a terminal and type:

nc localhost 1234

…except that’s probably going to just quit, since you don’t actually
ever
write anything, as far as I can tell.

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).

Since it’s a GServer example, I’m guessing you want something like
netcat.

Also, it looks like the “serve” method is designed to be an entire TCP
connection, so if you were intending this to be a persistent connection
(as
you’d have with a chat server), you’d need some sort of loop, as in the
original code.

Hi David!!

Thanks for your reply, I didn’t think about the very fact that I was
actually trying to implement a tcp protocol on a web browser. Thanks for
making me realize that. I guess now I can understand why that code in
the “example” is not working for me beyond a point… I was able to
implement a chat application using UDP Datagrams and UDPSocket class in
Ruby…