This module lets you easily interact with the Pusher REST API from your Play application.
Pusher is a hosted API for quickly, easily and securely adding scalable realtime functionality via WebSockets to web and mobile apps.
If you would like to know how Pusher works, check out the Pusher Documentation for more information.
Using it is very simple :
play install pusher
Then update your dependencenies.yml :
- play -> pusher 0.1
You can now easily interact with Pusher in your application
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
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 :
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.
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.