0.0
No commit activity in last 3 years
No release in over 3 years
Provide a really simple interface to manage events in ruby and via websockets using ruby and event machine.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Sock::Drawer

This gem allows message async message calls to subscribed listeners. Messages can be fired between ruby objects or to websocket connections.

Circle CI Code Climate Test Coverage

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

  1. Fork it ( https://github.com/HParker/sock-drawer/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. 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.