This module lets you easily interact with the Pusher Service within your Play 2 application.
To use this module on your Play 2 application, you need to modify your Build.scala
file:
val appDependencies = Seq( (... your other dependencies, if any ...) "tindr" % "play2pusher_2.9.1" % "1.0.1" // Tindr's play2pusher module dependency ) val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( (... your other settings, if any ...) resolvers += Resolver.url("Tindr's Play module repository", url("http://tindr.github.com/releases/")) (Resolver.ivyStylePatterns) )
That’s it!
Next time you build your project, it will fetch the module from our repository and add it to your project’s dependencies.
Take a look at the Play2Pusher-sample
project for a short example on how to use the module.
The preferred usage is as follows:
// Trigger an event on a channel and deliver a message to all the connected sockets:
val pusher = Pusher()
val promise = pusher.trigger(channel, event, message)
Which requires adding the following lines to your application.conf file:
# Pusher Configuration
pusher.appId = APP_ID
pusher.key = KEY
pusher.secret = SECRET
You could also do the following instead of using the application.conf file:
val pusher = Pusher(APP_ID, KEY, SECRET)
val promise = pusher.trigger(channel, event, message)
The Pusher.trigger method returns a Promise[Response], which you can manipulate to verify the response status and body for error handling.
Successful events will respond with a 202 status. For more details, take a look at http://www.pusher.com/docs/rest_api
You can also exclude a socket (usually the sender) from receiving the message by specifying its socket id:
pusher.trigger(channel, event, message, socketId)
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. This is achieved by:
pusher.createAuthString(socketId, channelName)
In your controller you can have something like this:
val socketId = "yourSocketId"
val channelName = "yourChannelName"
renderJSON(pusher.createAuthString(socketId, channelName))
You might need to turn off Pusher notifications altogether for testing or debugging.
The pusher.enabled=false
configuration option allows you to do just this. It defaults to true if not found.
- Support Presence channels properly.
- Add samples for private and presence channels.