CelluloidPubsub is a simple ruby implementation of publish subscribe design patterns using celluloid actors and websockets.
MIT License
CelluloidPubsub is a simple ruby implementation of publish subscribe design patterns using celluloid actors and websockets, using Celluloid::Reel server
Starting with version 0.9.0, the webserver will automatically detect an unused port instead of trying to connect by default to 1234 port. This solves some issues when this port is already used.
Starting with version 0.7.0, Support for Celluloid 0.17 has been added.
Starting with version 0.6.0, Redis support was moved into gem celluloid_pubsub_redis_adapter
Rails >3.0 only. MRI 1.9.x, 2.x, JRuby (--1.9).
Ruby 1.8 is not officially supported. We will accept further compatibilty pull-requests but no upcoming versions will be tested against it.
Rubinius support temporarily dropped due to Rails 4 incompatibility.
Add the following to your Gemfile:
gem "celluloid_pubsub"
Please read Release Details if you are upgrading. We break backward compatibility between large ticks but you can expect it to be specified at release notes.
Creating a websocket server is simple as doing this. This are all the options available with their default values.
CelluloidPubsub::WebServer.supervise_as(:web_server,
enable_debug: true, # if debug messages should be logged
adapter: nil , # if set to nil, will instantiate a simple Reactor to handle the connections which has no dependencies . Otherwise will try to use that adapter. Please see [celluloid_pubsub_redis_adapter](https://github.com/bogdanRada/celluloid_pubsub_redis_adapter) for more details on using redis adapter
log_file_path: "path/to/log_file.log", # The log file where all debugging information will be printed
hostname: "0.0.0.0", # the hostname of the server.
port: 1234, # the port on which the server will listen for connections
path: '/ws', # the relative path used in the URL where connections are allowed to connect
spy: false, # whether to spy all internal Websocket connections in order to get more debugging information
backlog: 1024 # the number of connections allowed to be connected to the server at a certain time
)
Creating a client is simple as doing this. If you provide the channel when you initialize the CelluloidPubsub::Client it will automatically start the subscription to that channel. But sometimes, you might want to subscribe at a later time, so you can just omit the channel when you initialize the client, and use instead @client.subscribe('test_channel'). After the subscription has started, the client must implement the method on_message and the on_close method (called when client disconnects from the channel). The method on_message will receive all incoming messages from the server. You can test if the subscription was successful by doing this @client.succesfull_subscription?(message).
class MyAwesomeClient
include Celluloid
def initialize(options = {})
@client = CelluloidPubsub::Client.new({
actor: Actor.current,
channel: 'test_channel', # the channel to which this client will subscribe to.
log_file_path: "path/to/log_file.log", # The log file where all debugging information will be printed
hostname: "0.0.0.0", # the hostname of the server.
port: 1234,# the port on which the connection will be made to
path: '/ws', # the relative path used in the URL where the connection will be connecting to
enable_debug: false # if debug messages should be logged
}.merge(options))
end
def on_message(message)
if @client.succesfull_subscription?(message)
puts "subscriber got successful subscription #{message.inspect}"
@client.publish('test_channel2', 'data' => ' subscriber got successfull subscription') # the message needs to be a Hash
else
puts "subscriber got message #{message.inspect}"
end
end
def on_close(code, reason)
puts "websocket connection closed: #{code.inspect}, #{reason.inspect}"
terminate
end
end
The methods available that the CelluloidPubsub::Client instance can execute are:
Please check the Examples Folder. There you can find some basic examples.
To test, do the following:
Please log all feedback/issues via Github Issues. Thanks.
== Copyright
Copyright (c) 2015 bogdanRada. See LICENSE.txt for further details.