Lightweight ActionCable implementation.
Contains application logic (channels, streams, broadcasting) and also (optional) Rack hijack based server (suitable only for development and test due to its simplicity).
Compatible with AnyCable (for production usage).
Add this line to your application's Gemfile:
gem 'litecable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install litecable
Please, checkout Action Cable guides for general information. Lite Cable aims to be compatible with Action Cable as much as possible without the loss of simplicity and ligthness.
You can use Action Cable javascript client without any change (precompiled version can be found here).
Here are the differences:
-
Use
LiteCable::Connection::Base
as a base class for your connection (instead ofActionCable::Connection::Base
) -
Use
LiteCable::Channel::Base
as a base class for your channels (instead ofActionCable::Channel::Base
) -
Use
LiteCable.broadcast
to broadcast messages (instead ofActionCable.server.broadcast
) -
Explicitly specify channels names:
class MyChannel < LiteCable::Channel::Base
# Use this id in your client to create subscriptions
identifier :chat
end
App.cable.subscriptions.create('chat', ...)
Lite Cable comes with a simple Rack middleware for development/testing usage. To use Lite Cable server:
-
Add
gem "websocket"
to your Gemfile -
Add
require "lite_cable/server"
-
Add
LiteCable::Server::Middleware
to your Rack stack, for example:
Rack::Builder.new do
map '/cable' do
# You have to specify your app's connection class
use LiteCable::Server::Middleware, connection_class: App::Connection
run proc { |_| [200, { 'Content-Type' => 'text/plain' }, ['OK']] }
end
end
Lite Cable is AnyCable-compatible out-of-the-box. You should write a simple server script:
#!/usr/bin/env ruby
require "my_app"
require "rack"
require "anycable"
# Turn AnyCable compatibility mode
LiteCable.anycable!
Anycable.connection_factory = MyApp::Connection
Anycable::Server.start
And then run this script along with your application. See Sinatra example for more.
Lite Cable uses anyway_config for configuration.
See config for available options.
-
Channel callbacks (
after_subscribe
, etc) -
Stream callbacks (
stream_from "xyz" { |msg| ... }
) -
Periodical timers
-
Remote connections.
Bug reports and pull requests are welcome on GitHub at https://github.com/anycable/litecable.
The gem is available as open source under the terms of the MIT License.