Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Best way to integrate with Rails 3? #21

anand opened this Issue · 14 comments

5 participants

anand Bernard Potocki Ilya Grigorik Adrià Casajús Martyn Loughran

When I run the code within examples/echo.rb in a Rails 3 initializer, it errors out on the first message with a process_data error. The same code runs fine outside Rails in a standalone program. What could be the issue here ?

em-websocket/framing76.rb:67:in `process_data': 134217727: /\A\x00([^\xff]*)\xff/ (SyntaxError)

(The above is a simple test case. It actually needs to be done within a thread so that the rest of the rails 3 initialization proceeds. This was just a test case which was on jRuby; client was chrome running examples/test).

On a related note, what is the best practice to integrate em-websocket within Rails 3 for just sending notification messages via a web service?

Bernard Potocki

About integration with Rails 3 - you could use already available solutions build on top of em-websocket like open source Socky or commercial Pusher


Thanks for the reply, imanel. Socky runs within a seperate process, right ?

I wanted the EventMachine reactor loop within the main process space (running within a thread as recommended in one of the EM presentations.) for certain other tasks as well. Any thoughts on that ?

Bernard Potocki

Indeed - Socky runs within a separate process. And personally I think it's better than merging it with webserver process. Main reason is that when you will update web app you will not need to disconnect all users from websocket server. Another one is that if you will handle automatic spawning of thread from web app server like passenger then you will end with multiple instances of websocket server trying to access the same port. Third one is that with one external server you will have all users on one server instead of multiple so it will be easier to broadcast message to all users. So even if you don't want to use any external tools it will be best to spawn external em-websocket instance and monit it with god or something like that...


Thanks for the comments, Imanel. Certainly these are valid reasons to run in a separate process.

I would still like to understand why em-websocket errors out on the particular line killing the EM.reactor_loop along with it when run like this.

Bernard Potocki

Could you provide value and class of @data before this error happens? I'm not sure, but if @data is '134217727'(I think that this number is value?) then it's possible that data is not String but rather Integer.

Also please edit stacktrace result line so it have 4 spaces at beginning(markdown "code" syntax) so Github won't change characters(it's impossible to guest what really was throwed) and give note so we can continue debuging.


Edited the code in the comment above to show code correctly. Will work on getting the value and class of @data next.

[[:message, "Issue 21"]]

[[:message, String]]

[[:message, "\000hello world!\377"]]

Here is the requested data - output generated using the debug keyword in the library and copied from the console. Data value does not seem to be significant here. Any value in the message from the client seems to be triggering the same error.

Ilya Grigorik

Anand, for a great example of Rails + em-websocket, take a look at the diaspora project:

They're using em-websocket under the hood to power a part of the app.

Bernard Potocki

Biggest problem with this code is that it is on AGPL, what means that if you use even smallest part of that code then you must release all code of your application with AGPL license. So if you plan to write any non-opensource project you are not allowed to base on that code - at last in matter of license.

Martyn Loughran mloughran closed this
Adrià Casajús

Diaspora no longer uses em-websocket (or any websocket that I can see) any longer. Is there a way on how to hijack a rails socket and pass it into em-websocket? That would be ideal for me.

Martyn Loughran

I have not tried to do this, so I have no idea, but I would advise against this approach.

Adrià Casajús


Martyn Loughran

Loads of extra layers of complexity, more stuff to go wrong, em-websocket was not designed with this in mind, and afaik is not run like this in production. Feel free to work out the details and post on the wiki though. You could also try which works with Rack.

Adrià Casajús

You don't run it in production like this, but maybe your use case is different from mine. Pusher seems to be ws oriented but I need a mixture of architectures. Plain HTTP + ws. I can't afford to create a thread per ws (that won't scale) but I don't want to duplicate all my authz stuff in another server that is not rails. So faye-... doesn't really help me.

Since my ws connections are really low traffic I want to delegate those to a thread running EM to handle those while the rest of rails keeps doing threads to "schedule" db access that can't be evented.

In any case I've almost got an implementation directly using EM+websocket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.