This extension allows a faye client to retrieve its client id when restarting, after a crash, etc; thus retrieving all the messages addresses to it sent during the disconnection.
Add this line to your application's Gemfile:
gem 'faye-reconnect'
And then execute:
$ bundle
Or install it yourself as:
$ gem install faye-reconnect
To require it in your application :
require 'faye/reconnect'
Faye::Reconnect
is actually two faye extensions, one for the Faye server and one for the clients.
You just need to add the extension and pass your faye server to it :
server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 15)
server.add_extension Faye::Reconnect::ServerExtension.new(server)
The client extension has a dependency on redis. It uses redis to persist the client id after each successful handshake so it can re-use it when trying to reconnect.
Add the extension to your faye client :
client = Faye::Client.new('http://localhost:9292/faye')
client.add_extension Faye::Reconnect::ClientExtension.new({
name: 'your_client_name',
redis: {
host: 'localhost',
port: 6379,
password: '',
database: 0
}
})
:name
is mandatory, it is used to distinct clients.
If you don't specify redis options, the ones provided in the above example will be used.
You may already have something along the lines of :
trap('TERM') do
client.disconnect
end
If you plan on reconnecting with the same client id and get missing messages, you have to use the stop!
method provided by this gem :
trap('TERM') do
client.stop!
end
Instead of sending a /meta/disconnect
message to the server, it will cleanly stop the Event Machine reactor, so the faye server is not aware you are disconnected and it will keep incoming messages until you handshake again.
- Fork it (https://github.com/jarthod/faye-reconnect/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