Pandapush

Gem Version

Pandapush is an event push system similar to Pusher for internal Instructure use.

Installation

Install the gem on your machine:

gem install pandapush

Or add it to your Gemfile:

gem 'pandapush'

Note: Pandapush 1.1.0+ requires Ruby 2.1+ due to its use of required named parameters.

Setup

Create /config/initializers/pandapush.rb and add:

Pandapush.app_id = 'YOUR APP ID'
Pandapush.key = 'YOUR APP KEY'
Pandapush.secret = 'YOUR APP SECRET'

Usage

To publish a message to /{your app id}/private/messages:

client.publish(channel: 'messages', message: {user: {id: 1, name: 'Neil Gupta', email: '[email protected]'}})

To generate a token for a JS client to publish (but not subscribe) to /{your app id}/private/messages:

Pandapush.generate_token(channel: 'messages', sub: false)

If, for some reason, you need to use multiple Pandapush API keys, you can also initialize your own client with

client = Pandapush::Client.new(
  app_id: 'YOUR APP ID',
  key: 'YOUR APP KEY',
  secret: 'YOUR APP SECRET'
)

client.publish(channel: 'messages', message: {user: {id: 1, name: 'Neil Gupta', email: '[email protected]'}})

Documentation

Pandapush.publish(channel:, message, channel_type: 'private')

Sends a message to a Pandapush channel.

  • channel - The name of the channel to push to (required)
  • message - hash that will be JSON encoded (required)
  • channel_type - 'public' or 'private'. Defaults to private

Example:

Pandapush.publish(channel: 'messages', message: {user: {id: 1, name: 'Neil Gupta', email: '[email protected]'}})

Note: channel is a string and can be any valid uri, such as 'users/1'. Pandapush will handle generating the URL with your app id and channel type.

Pandapush.generate_token(channel:, channel_type: 'private', pub: true, sub: true, presence: nil)

Generates a JWT token for client authentication.

  • channel - The channel name the token works for (required)
  • channel_type - 'public', 'private', or 'presence'. Defaults to private
  • pub - true if this token allows publishing. Defaults to true
  • sub - true if this token allows subscribing. Note that sub on a public channel is redundant. Defaults to true
  • presence - A hash used to identify a user when subscribing to a presence channel. Must have at least an id key.

Example:

Pandapush.generate_token(channel: 'messages')

Pandapush.app_id

Getter and setter for your app id.

Pandapush.key

Getter and setter for your app key.

Pandapush.secret

Getter and setter for your app secret.

Pandapush.url

Getter and setter to override the default url we use for Pandapush. Useful for running dockerized pandapush.

All methods can also be accessed from Pandapush::Client.new.