Providing A Custom Channels Authoriser

It is possible to modify the default authorisation behaviour of our client libraries by providing a custom authoriser. Consider the following motivating scenario:

Normally when subscribing to a private or presence channel we first send a request to our auth endpoint to get authentication data, and then only after that do we make the call to the Pusher servers to register the subscription. If we know in advance which channels we might need to subscribe to, it would speed up the eventual subscription if we could pre-populate the auth data, and send the subscription request to Pusher immediately upon firing pusher.subscribe(…).

We can do this with a custom authoriser that checks for pre-populated auth data, and provides that immediately.

In pusher-js a custom authoriser is of the form

function ourCustomAuthorizer (context, socketId, callback) {
  // get auth data somehow
  // if all goes well:
  callback(false, authData);
  // otherwise:
  callback(true, "some error string describing what went wrong");

We can then use this by adding it to the return value of the Pusher.Runtime.getAuthorizers function.

var supportedAuthorizers = Pusher.Runtime.getAuthorizers();

supportedAuthorizers.ourCustomAuthorizer = ourCustomAuthorizer;

Pusher.Runtime.getAuthorizers = function () {
  return supportedAuthorizers;

And now we can use it by setting the authTransport in our options when we initiate the Pusher object.

var pusher = new Pusher("YOUR_KEY", {
  authTransport: "ourCustomAuthorizer",
  encrypted: true

For a full example solving the motivating scenario above, see here.

Still have questions? Please reach out to our Support team by visiting this page.

Last updated