Giter Club home page Giter Club logo

pusher-gem's Introduction

Pusher gem

Getting started

After registering at http://pusher.com configure your app with the security credentials

Pusher.app_id = 'your-pusher-app-id'
Pusher.key = 'your-pusher-key'
Pusher.secret = 'your-pusher-secret'

Trigger an event with {Pusher::Channel#trigger!}

Pusher['a_channel'].trigger!('an_event', {:some => 'data'})

Handle errors by rescuing Pusher::Error (all Pusher errors are descendants of this error)

begin
  Pusher['a_channel'].trigger!('an_event', {:some => 'data'})
rescue Pusher::Error => e
  # (Pusher::AuthenticationError, Pusher::HTTPError, or Pusher::Error)
end

Optionally a socket id may be provided. This will exclude the event from being triggered on this socket id (see http://pusher.com/docs/publisher_api_guide/publisher_excluding_recipients for more info).

Pusher['a_channel'].trigger!('an_event', {:some => 'data'}, socket_id)

If you don't need to handle failure cases, then you can simply use the {Pusher::Channel#trigger} method, which will rescue and log any errors for you

Pusher['a_channel'].trigger('an_event', {:some => 'data'})

Logging

Errors are logged to Pusher.logger. It will by default use Logger from stdlib, however you can assign any logger:

Pusher.logger = Rails.logger

Asynchronous triggering

To avoid blocking in a typical web application, you may wish to use the {Pusher::Channel#trigger_async} method which makes asynchronous API requests. trigger_async returns a deferrable which you can optionally bind to with success and failure callbacks.

You need to be running eventmachine to make use of this functionality. This is already the case if, for example, you're deploying to Heroku or using the Thin web server. You will also need to add em-http-request to your Gemfile.

$ gem install em-http-request

deferrable = Pusher['a_channel'].trigger_async('an_event', {
  :some => 'data'
}, socket_id)
deferrable.callback {
  # Do something on success
}
deferrable.errback { |error|
  # error is a instance of Pusher::Error
}

Private channels

The Pusher Gem also deals with signing requests for authenticated private channels. A quick Rails controller example:

reponse = Pusher['private-my_channel'].authenticate(params[:socket_id])
render :json => response

Read more about private channels in the docs and under {Pusher::Channel#authenticate}.

Developing

Use bundler in order to run specs with the correct dependencies.

bundle
bundle exec rspec spec/*_spec.rb

Copyright

Copyright (c) 2010 New Bamboo. See LICENSE for details.

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.