5

I'm writing a simple chat room application in Rails 3.1 - for learning purposes. For starters I have all the needed models (messages, users, rooms, etc.) and things work great. The clients poll the server every minute (for example) and get new messages if they have any.

I would like to change the simple polling to long polling and can't figure out if this can be done in the same app or do I have to create some other Push server for the long polling.

I read a lot about EventMachine and changed my rails app to user it as I wanted to use EventMachine for the event driven mechanics. I thought that the EventMachine channel would come in handy for this. A client would connect and wait for a message in the chat room and it will receive a message only when one was sent to the room.

What I can't figure out is how can I share the EventMachine::Channel instance between all my client connections. Is this approach even possible or am I going at it the wrong way?

If possible I would like a solution that can run as a single rails application hosted on Heroku.

Oded
  • 233
  • 2
  • 9
  • I'm not an expert on event machine, but I think it's possible. And you don't share the channel between your clients, but you have one per client and you need to build something on top of EM to send messages between the different users. – Augusto Nov 22 '11 at 11:49
  • Yes, say I have a channel for every client. Where can I store all those channel instances so it will be accessible from a controller that is invoked when someone else sends a message? – Oded Nov 22 '11 at 12:03

3 Answers3

1

Yeah sure. I just wrote a demo using event machine. My case is that player walking around a map, and other players should be able to see it. The demo looks like that:

  1. A client establishes a connection, reporting its own coordinate(generated randomly)

  2. There is an array preserving all the coordinates for each client

  3. When a client moves, it sends its new coordinate to the server. Then the server finds out people near him(from the array), and push the new coordinate to those clients.

I tested it with nearly 5000 clients, and each second 20-30 players moves its position. And the server process only takes less that 100M memory & 50%-60% cpu usage(on a single core).

In your case, I think you should probably try faye too. It's based on event machine, and an appropriate solution to things like chat room.

Dean Winchester
  • 629
  • 5
  • 17
0

Rails will have streaming added in version 4. For now, you can streaming (long polling) like in this example with Sinatra and Redis's Pub/Sub feature as a backend. You will have to add another action to handle user sent messages, adding them to Redis's with PUBLISH command. You should use an evented server like Thin or Puma.

phil pirozhkov
  • 4,740
  • 2
  • 33
  • 40
0

Expanding what I've mentioned on the comment, check this blog post that explains how to create a text based chat app using EM, and uses AMQP to broadcast the messages to the other users.

I think you can probably do the same or use some in memory queues to share messages, and this definitely should work on heroku, as you don't have a dependency to an external service such as RabbitMQ. Here's a good discussion about different queue frameworks: ActiveMQ or RabbitMQ or ZeroMQ or

Community
  • 1
  • 1
Augusto
  • 28,839
  • 5
  • 58
  • 88