Skip to content

alejandrolujan/Play2Pusher

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Pusher module for Play! 2

This module lets you easily interact with the Pusher Service within your Play 2 application.

Installation

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.

Usage

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)

Examining the response

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

Excluding sockets

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

pusher.trigger(channel, event, message, socketId)

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. This is achieved by:

pusher.createAuthString(socketId, channelName)

Example

In your controller you can have something like this:

val socketId = "yourSocketId"
val channelName = "yourChannelName"
renderJSON(pusher.createAuthString(socketId, channelName))

Disabling Pusher events

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.

Pending work

  1. Support Presence channels properly.
  2. Add samples for private and presence channels.

About

Play! 2 Module for the Pusher service.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published