README.md in jugglite-0.5.0 vs README.md in jugglite-0.6.0

- old
+ new

@@ -18,32 +18,51 @@ $ gem install jugglite ## Server Usage -I use jugglite as rack middleware in development and as a standalone binary behind nginx in production. +I used to use Jugglite as rack middleware in development and as a standalone binary behind nginx in production. Nowadays I run my rails application using thin in production so I can mount Jugglite in the routes.rb file. +### Inside Rails's routes.rb + +This only works with an EventMachine based webserver that supports rack's async.callback. I have only tested this in production with Thin, but it might work with Rainbows or Puma. + +This setup is great because it allows you to do channel authorization on a per request basis. + +```ruby +# config/routes.rb +# ... + +@allowed_channels = ->(request) { + user_id = request.session['user_id'] + user_id ? ['broadcast', "player_#{user_id}"] : [] +} +get 'stream', to: Jugglite::App.new(nil, namespace: "app:#{Rails.env}:", allowed_channels: @allowed_channels) + +# ... +``` + ### Stand-alone binary Jugglite comes with a binary. This binary runs a thin server that listens on redis for application messages and passes it along to all connected clients. You can run the binary from any terminal like this (these options are the defaults): `jugglite --address 0.0.0.0 --port 3000 --max-conns 1024` -### As Rack middleware (development) +### As Rack middleware Add it to your `config.ru` file and make sure your application runs using Thin: ```ruby require ::File.expand_path('../config/environment', __FILE__) # Embed Jugglite when running in development use Jugglite::App, path: '/stream', namespace: 'myapp:' if ENV['RACK_ENV'] == 'development' run MyRails::Application ``` -### Behind Nginx (production) +### Behind Nginx NOTE: because the html5 SSE implementation requires the connection to have the same hostname and port, you'll need to add a reverse proxy in front of your app and jugglite. This is an example nginx configuration with Unicorn and Jugglite. Make sure you set `proxy_buffering off;` in your Nginx configuration. @@ -87,10 +106,9 @@ proxy_redirect off; proxy_pass http://unicorn-example; } } ``` - ## Client Usage Use the browser's native [Server-Sent Events](http://www.html5rocks.com/en/tutorials/eventsource/basics/) implementation: