Oded
Oded

Reputation: 233

Is long polling possible with a Rails application using EventMachine?

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.

Upvotes: 5

Views: 2874

Answers (3)

Dean Winchester
Dean Winchester

Reputation: 659

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.

Upvotes: 1

phil pirozhkov
phil pirozhkov

Reputation: 4900

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.

Upvotes: 0

Augusto
Augusto

Reputation: 29827

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

Upvotes: 0

Related Questions