简体   繁体   中英

Rails using Websockets with Nginx and Unicorn?

I am considering implementing chess (which needs websockets) with Rails, and in production deployment using Nginx as a reverse proxy to a bunch of Unicorn processes.

In thinking about how to make that work led me to have the following questions:

As far as I understand websockets are a persistent connection. Since everything goes through the reverse proxy Nginx how exactly would a Unicorn worker process maintain a websocket connection to a client browser? Would Nginx maintain state about which Unicorn process each browser websocket is connected to and act as a kind of intermediary? Does keeping a persistent websocket connection in a Unicorn process block the entire worker process?

Is there a recommended way to implementing chess (with websockets) using Rails?

Connecting synchronous processing by Unicorn with asynchronous delivery using nginx would imply some logic on nginx side that seems at least awkward to me. At most - impossible.

There is a Railscast about Private Pub gem that makes use of Thin webserver. It's way more suitable for this task: it's asynchronous, it's able to handle many concurrent requests with event-based IO. So I suggest you replace Unicorn with Thin or install Thin side-by-side.

Puma webserver might also be an option, however, I can't give more info about that.

nginx won't do websockets. Are you sure you can't do this with AJAX? If you really need push capability you could try something built around the Comet approach: http://en.wikipedia.org/wiki/Comet_(programming)

Faye is a pretty good gem for implementing comet in rails: http://faye.jcoglan.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM