views:

868

answers:

2

I am building a chat system using EventMachine and ruby on rails. It's for learning purpose.

This is how client is connecting to server.

c = TCPSocket.open(ip_address, port)
data = {:user_id => 2, :message => 'hello world'}
c.send(data)
response = c.gets
c.close

It works. However the problem is that I can't get the list of people who are currently chatting in the room because as I shown above, client is constantly opening and closing the connection.

An alternative plan is to run an EventMachine client for each connected user. I am planning to store the client connection in session for each user. In this way I will be using the same question for each user. When the user logs out then I will close the connection.

However if the user walks out then how do I self close the client connection.

Any thoughts.

A: 

If the messages are stored within the database, then query it for the users that have written a message in the last 5 minutes. That way if they idle for more than five minutes they're automatically considered out of the chatroom.

The Wicked Flea
+2  A: 

Instead of writing your own, you may be able to build on the Juggernaut library. This is an EventMachine framework which has an example that does precisely this sort of thing.

tadman