Pusher-js Batch Authentication Plugin

4.0.1 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Pusher client with the `PusherBatchAuthorizer` and subscribe to multiple channels, showcasing how the plugin automatically batches authentication requests.

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.');

view raw JSON →