Sock::Drawer
This gem allows message async message calls to subscribed listeners.
Messages can be fired between ruby objects or to websocket connections.
Installation
Add this line to your application's Gemfile:
gem 'sock-drawer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sock-drawer
Usage
Initialize a instance of the sock-drawer client
sock = Sock::Client.new(logger: Rails.logger, redis: redis)
Publishing
Publish an event on a channel,
sock.pub("my message", channel: "my-channel")
or publish to all channels,
sock.pub("my message")
Receiving in Javascript
To capture the event in Javascript use something like,
var webSocket = new WebSocket("ws://" + location.hostname + ":8081/" + "my-channel");
webSocket.onmessage = function(event) {
console.log(event.data);
}
Subscribing
Create a class to handle redis events like,
class MyListener
include Sock::Subscriber
on 'echo' do |msg|
msg
end
end
Then register your listener with the server
Sock::Server.new(listener: MyListener)
Whenever an event is fired on the sock-hook/echo
channel the block will be executed.
Configuration
you can configure your sock server to run as a rake task like,
namespace :sock do
desc 'start the sock-drawer server to manage socket connections'
task :server do
Sock::Server.new.start!
end
end
Then run it with rake sock:server
Current supported configuration options:
keyword arg | default |
---|
name | 'sock-hook' |
logger | Logger.new(STDOUT) |
socket_params | { host: '0.0.0.0', port: 8020 } |
mode | 'default' |
listener | N/A |
Wish List
- Right now all configuration is passed into new, it would be nice to read from a config file
- There isn't a way of having multiple event handlers. Should be easy to pass multiple in or intelligently find them (given some convention)
And you are good to go!
Contributing
- Fork it ( https://github.com/[my-github-username]/sock-drawer/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
Running the tests
if you are going to contribute, I hope you run the tests at least once -- hopefully many times.
to run the tests, you must have redis-server running in the background with default configuration.