Ruby WebSocket client for Pusher Channels
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
certs Updated cacert file Oct 22, 2013
examples Only require logger if necessary Oct 22, 2013
lib Use the socket specified logger for the Channels object also May 21, 2015
spec Fix tests Jan 4, 2015
.document Initial commit to pusher-client. Dec 29, 2010
.gitignore Ignoring Gemfile.lock Mar 6, 2013
.travis.yml Travis-CI: remove rubinius again since it keeps failing Mar 21, 2014
CHANGELOG.md Release v0.6.2 May 14, 2015
Gemfile Don't special-case the json dependency. It's always needed. Mar 20, 2014
LICENSE.txt
README.md Show how to configure cluster May 3, 2018
Rakefile Migrate tests to rspec. Bacon wasn't failing properly on error Mar 21, 2014
pusher-client.gemspec Allow websocket to be any 1.x version Apr 17, 2014

README.md

pusher-client: Ruby WebSocket client for Pusher Channels

Build Status

pusher-client is a Ruby gem for consuming WebSockets from the Pusher Channels web service.

Installation

gem install pusher-client

This gem is compatible with jruby since 0.2.

Single-Threaded Usage

The application will pause at channels_client.connect and handle events from Pusher Channels as they happen.

require 'pusher-client'
cluster = 'mt1'  # take this from your app's config in the dashboard
channels_client = PusherClient::Socket.new(YOUR_APPLICATION_KEY, {
  secure: true,
  ws_host: "ws-#{cluster}.pusher.com"
})

# Subscribe to two channels
channels_client.subscribe('channel1')
channels_client.subscribe('channel2')

# Subscribe to presence channel
channels_client.subscribe('presence-channel3', USER_ID)

# Subscribe to private channel
channels_client.subscribe('private-channel4', USER_ID)

# Subscribe to presence channel with custom data (user_id is mandatory)
channels_client.subscribe('presence-channel5', :user_id => USER_ID, :user_name => 'john')

# Bind to a global event (can occur on either channel1 or channel2)
channels_client.bind('globalevent') do |data|
  puts data
end

# Bind to a channel event (can only occur on channel1)
channels_client['channel1'].bind('channelevent') do |data|
  puts data
end

channels_client.connect

Asynchronous Usage

With channels_client.connect(true), the connection to Pusher Channels will be maintained in its own thread. The connection will remain open in the background as long as your main application thread is running, and you can continue to subscribe/unsubscribe to channels and bind new events.

require 'pusher-client'
channels_client = PusherClient::Socket.new(YOUR_APPLICATION_KEY)
channels_client.connect(true) # Connect asynchronously

# Subscribe to two channels
channels_client.subscribe('channel1')
channels_client.subscribe('channel2')

# Bind to a global event (can occur on either channel1 or channel2)
channels_client.bind('globalevent') do |data|
  puts data
end

# Bind to a channel event (can only occur on channel1)
channels_client['channel1'].bind('channelevent') do |data|
  puts data
end

loop do
  sleep(1) # Keep your main thread running
end

Using native WebSocket implementation

This gem depends on the websocket gem which is a pure Ruby implementation of websockets.

However it can optionally use a native C or Java implementation for a 25% speed increase by including the websocket-native gem in your Gemfile.

Copyright and license

See LICENSE.txt.