Pusher-js Batch Authentication Plugin
Pusher-js-auth is a client-side plugin designed to enhance the official `pusher-js` library by batching multiple private and presence channel authentication requests into a single HTTP call. This significantly improves performance and reduces network overhead when an application subscribes to numerous channels concurrently. The current stable version is 4.0.1, which is compatible exclusively with `pusher-js` versions 7.x and later. Older `pusher-js` versions (6.x and below) require the 3.x release of this plugin. While the project's release cadence is tied to major `pusher-js` updates, it provides a consistent solution for optimizing authentication workflows. Key differentiators include its seamless integration via the `authorizer` option in the `Pusher` client, and configurable `authDelay` for managing request timing. It requires a custom server-side authentication endpoint capable of processing batched channel names and returning a consolidated JSON response, which Pusher's server libraries can facilitate.
Common errors
-
Pusher: Auth channel failed for private-channel (403)
cause The server-side authentication endpoint did not provide a valid batched response for one or more channels, or explicitly rejected authentication for a specific channel (e.g., with a 403 HTTP status).fixVerify your server-side authentication endpoint's logic. Ensure it correctly parses the batched channel names from the request and returns a JSON object with valid authentication data or appropriate error statuses for each requested channel, following the `pusher-js-auth` expected output format. -
TypeError: PusherBatchAuthorizer is not a constructor
cause The `PusherBatchAuthorizer` symbol was not correctly imported or loaded before being passed to the `Pusher` client constructor, or it's being used with an incompatible `pusher-js` version.fixEnsure `pusher-js-auth` is correctly installed (`npm install pusher-js-auth`) and imported (`import { PusherBatchAuthorizer } from 'pusher-js-auth';` or `const { PusherBatchAuthorizer } = require('pusher-js-auth');`). Additionally, confirm that your `pusher-js` version is 7 or higher if you are using `pusher-js-auth` v4+.
Warnings
- breaking Pusher-js-auth v4.x is explicitly incompatible with `pusher-js` versions 6.0 and older. Using this version of the plugin with an older `pusher-js` client will lead to authentication failures.
- gotcha Your server-side authentication endpoint must be designed to handle batched authentication requests. It expects an array of `channel_name` parameters and should return a batched JSON response.
- gotcha The `authDelay` option (defaulting to 0 milliseconds) allows fine-tuning when authentication requests are sent. While it can be as low as 0, the very first authentication request is always internally postponed until the connection to Pusher is successfully established, regardless of this setting.
Install
-
npm install pusher-js-auth -
yarn add pusher-js-auth -
pnpm add pusher-js-auth
Imports
- PusherBatchAuthorizer
import PusherBatchAuthorizer from 'pusher-js-auth';
import { PusherBatchAuthorizer } from 'pusher-js-auth'; - PusherBatchAuthorizer
const PusherBatchAuthorizer = require('pusher-js-auth');const { PusherBatchAuthorizer } = require('pusher-js-auth');
Quickstart
import Pusher from 'pusher-js'; // Assuming 'pusher-js' is installed
import { PusherBatchAuthorizer } from 'pusher-js-auth';
// Your Pusher app key (replace with environment variable in production)
const PUSHER_APP_KEY = process.env.PUSHER_APP_KEY ?? 'YOUR_APP_KEY';
// Your Pusher cluster (e.g., 'us2', 'eu')
const PUSHER_APP_CLUSTER = process.env.PUSHER_APP_CLUSTER ?? 'YOUR_CLUSTER';
// The endpoint on your server that handles batch authentication
const AUTH_ENDPOINT = process.env.PUSHER_AUTH_ENDPOINT ?? '/pusher/batch-auth';
// Initialize Pusher client with the batch authorizer
const pusher = new Pusher(PUSHER_APP_KEY, {
cluster: PUSHER_APP_CLUSTER,
authorizer: PusherBatchAuthorizer,
authDelay: 200, // Optional: delay in milliseconds before executing auth request
authEndpoint: AUTH_ENDPOINT // Ensure this points to your server-side batch auth handler
});
// Subscribe to multiple private and presence channels
// The PusherBatchAuthorizer will automatically batch authentication requests for these
// into a single HTTP call to the authEndpoint.
const privateChannel1 = pusher.subscribe('private-chat-1');
const privateChannel2 = pusher.subscribe('private-messages-user-abc');
const presenceChannel = pusher.subscribe('presence-online-users');
// Example of binding to an event on a channel
privateChannel1.bind('client-message', (data: any) => {
console.log('Received message on private-chat-1:', data);
});
presenceChannel.bind('pusher:member_added', (member: any) => {
console.log(`Member added to presence-online-users: ${member.id}`);
});
console.log('Pusher client initialized and channels subscribed. Authentication requests will be batched.');