Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 409
Oded Avatar asked Nov 13 '22 14:11

Oded


1 Answers

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.

like image 63
Dean Winchester Avatar answered Dec 20 '22 02:12

Dean Winchester