Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Websockets not working in my Rails app when I run on Unicorn server, but works on a Thin server

websockets websocket-rails heroku unicorn thin

  • Please log in to reply
4 replies to this topic

#1 beeeph

beeeph

    Passenger

  • Members
  • 3 posts

Posted 04 September 2014 - 04:58 AM

I'm learning Ruby on Rails to build a real-time web app with WebSockets on Heroku, but I can't figure out why the websocket connection fails when running on a Unicorn server. I have my Rails app configured to run on Unicorn both locally and on Heroku using a Procfile...
    web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
which I start locally with `$foreman start`. The failure occurs when creating the websocket connection on the client in javascript...
    var dispatcher = new WebSocketRails('0.0.0.0:3000/websocket'); //I update the URL before pushing to Heroku
with the following error in the Chrome Javascript console, 

and when I run it on Unicorn on Heroku, I get a similar error in the Chrome Javascript console, 

The stack trace in the Heroku logs says, 
RuntimeError (eventmachine not initialized: evma_install_oneshot_timer):
What's strange is that it works fine when I run it locally on a Thin server using the command `$rails s`. 
 
I've spent the last five hours researching this problem online and haven't found the solution. Any ideas for fixing this, or even ideas for getting more information out of my tools, would be greatly appreciated!


#2 beeeph

beeeph

    Passenger

  • Members
  • 3 posts

Posted 04 September 2014 - 05:09 AM

I would love to show you what the actually errors are in the Javascript console, but every time I edit the post to include them, they're hidden/deleted for some unknown reason. Very frustrating.

 

They errors basically say the websocket connection failed, related to the handshake getting screwed up.



#3 beeeph

beeeph

    Passenger

  • Members
  • 3 posts

Posted 08 September 2014 - 01:40 AM

A key difference in the two web servers I'm using seems to be that one (Thin) is EventMachine-based and one (Unicorn) is not. The Websocket Rails project wiki states that a Standalone Server Mode must be used for non-EventMachine-based web servers such as Unicorn (which would require an even more complex setup on Heroku as it requires a Redis server). The error message RuntimeError (EventMachine not initialized: evma_install_oneshot_timer): suggests that standalone-mode was not used.

Heroku AFAIK only exposes one internal port (exposed as an environmental variable) externally as port 80. A WebSocket server normally requires its own socket address (port number) (which can be worked around by reverse-proxying the WebSocket server). Websocket-Rails appears to get around this limitation by hooking into an existing EventMachine-based web server (which Unicorn does not provide).



#4 Andrey

Andrey

    Passenger

  • Members
  • 1 posts

Posted 04 October 2014 - 10:26 AM

I have the same problem with unicorn and heroku , do you have any progress ?



#5 tapan

tapan

    Passenger

  • Members
  • 1 posts

Posted 24 December 2014 - 08:00 AM

I get this error when trying to integrate unicorn and nginx : 2014/12/24 07:50:50 [error] 2020#0: *1 connect() to unix:/var/run/unicorn.abc.sock failed (111: Connection refused) while connecting to upstream, client: 2x.x.xx.xx, server: ec2-yy-yy-yy-yyy.compute-1.amazonaws.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run...corn.abc.sock:/", host: "ec2-yy-yy-yy-yyy.compute-1.amazonaws.com:4000"







Also tagged with one or more of these keywords: websockets, websocket-rails, heroku, unicorn, thin

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users