Shower allows the creation of an unlimited number of event streams through a single connection! Server side events have never been easier and more efficient. Since there is no needed to persist a database connection, the amount of concurrent users is only limited by your Rails server.
As with any application wishing to use ActionController::Live
, a multithreaded server is required. I recommend, Puma, it's open source and free. Passenger Enterprise is another option, it's a little simpler to setup then Puma.
A Redis server must be installed and running. Messages are pushed to a Redis server, where the Shower::StreamController
then picks them up and pushes them to the event stream. Because of this, the event stream does not need a database connection!
Add this line to your application's Gemfile:
gem 'shower'
And then execute:
$ bundle
After bundling you need to run the initial installation generator for javascript client usage:
$ rails g shower:install
Create config/initializers/redis.rb
and configure Redis connection:
$redis = Redis.new(host: '127.0.0.1', port: 6379)
Pushing messages to an event stream is simple, just pass the event name and the data. The data will be converted to
JSON using to_json
.
Shower::Stream.publish('message.new', { username: 'anon', message: 'hello!' })
Subscribing to an event stream on server side.
Shower::Stream.subscribe(['message.new']) do |event, data|
# do something with event
# do something with data
end
To listen for messages, use the JavaScript Shower
class. The constructor takes the path and an array of events.
stream = new Shower('/stream', ['message.new', 'alert.new'])
Now define a callback for each event.
stream.addEventListener('message.new', (event) ->
message = JSON.parse(event.data)
console.log 'new message:'
console.log message
)
stream.addEventListener('alert.new', (event) ->
alert(event.data)
)
- Fork it ( https://github.com/[my-github-username]/rails_script/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request