/Play-Pusher

A Play Framework plugin for the Pusher REST API.

Primary LanguageJava

Pusher module

This module lets you easily interact with the Pusher REST API from your Play application.

Usage

Using it is very simple :

Pusher pusher = new Pusher(appId, key, secret)

Or if you prefer you can add your application API access information to the application.conf file

#Pusher
pusher.appId = APP_ID
pusher.key = KEY
pusher.secret = SECRET

and do the following:

Pusher pusher = new Pusher();

Triggering an event on a channel

You can trigger an event on a channel and deliver a message to all the connected sockets:

HttpResponse response = pusher.trigger("my_channel", "event", "my_message");

This will return the following response if all goes well

202 ACCEPTED

You can also exclude a socket (usually the sender) from receiving the message by specifying its socket id:

pusher.trigger("my_channel", "event", "my_message", "socket_id");

Generating the authentication string

When a socket tries to connect to a private channel, it will make a POST request to /pusher/auth on your server. You will need to generate and return a JSON object containing the auth signature that allows the socket to connect to the private channel. That is really easy :

pusher.createAuthString(socketId, channelName);

Example

In your controller you can have something like this:

String socketId = params.get("socket_id);
String channelName = params.get("channel_name");
renderJSON(pusher.createAuthString(socketId, channelName));

You can also generate an auth signature to allow a socket to connect to a presence channel. In that case you will need to pass a PresenceChannelData object which has a userId field and a BasicUserInfo field.

Example

BasicUserInfo userInfo = new BasicUserInfo();
userInfo.setName("Joe");
PresenceChannelData channelData = new PresenceChannelData("userId", userInfo);
renderJSON(pusher.createAuthString(socketId, channelName, channelData));

Check out the Pusher Documentation for more information.