Git Product home page Git Product logo

pusher-ruby-client's Introduction

pusher-client (Ruby)

<img src=“https://travis-ci.org/pusher/pusher-ruby-client.png” />

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

The connection to Pusher can optionally be maintained in its own thread (see Asynchronous Usage).

This gem no longer depends on em-http, and is compatible with jruby since 0.2.

When binding to a global event, note that you still must be subscribed to the channels the event may be sent on. You can’t just bind a global event without subscribing to any channels and call it a day.

Installation

gem install pusher-client

Single-Threaded Usage

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

require 'pusher-client'
options = {:secret => 'YOUR_APPLICATION_SECRET'} 
socket = PusherClient::Socket.new(YOUR_APPLICATION_KEY, options)

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

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

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

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

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

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

socket.connect

Asynchronous Usage

The socket 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'
socket = PusherClient::Socket.new(YOUR_APPLICATION_KEY)
socket.connect(true) # Connect asynchronously

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

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

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

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

For further documentation, read the source & test suite. Some features of the JavaScript client are not yet implemented.

Native extension

Pusher 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.

Contributing to pusher-client

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2010 Logan Koester. See LICENSE.txt for further details.

pusher-ruby-client's People

Contributors

aaron1011 avatar gavinbunney avatar glenngillen avatar jarthod avatar jrozner avatar krisleech avatar leggetter avatar logankoester avatar mloughran avatar njh avatar pat avatar rkh avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.