{"library":"pusher-js-auth","title":"Pusher-js Batch Authentication Plugin","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install pusher-js-auth"],"cli":null},"imports":["import { PusherBatchAuthorizer } from 'pusher-js-auth';","const { PusherBatchAuthorizer } = require('pusher-js-auth');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Pusher from 'pusher-js'; // Assuming 'pusher-js' is installed\nimport { PusherBatchAuthorizer } from 'pusher-js-auth';\n\n// Your Pusher app key (replace with environment variable in production)\nconst PUSHER_APP_KEY = process.env.PUSHER_APP_KEY ?? 'YOUR_APP_KEY';\n// Your Pusher cluster (e.g., 'us2', 'eu')\nconst PUSHER_APP_CLUSTER = process.env.PUSHER_APP_CLUSTER ?? 'YOUR_CLUSTER';\n// The endpoint on your server that handles batch authentication\nconst AUTH_ENDPOINT = process.env.PUSHER_AUTH_ENDPOINT ?? '/pusher/batch-auth';\n\n// Initialize Pusher client with the batch authorizer\nconst pusher = new Pusher(PUSHER_APP_KEY, {\n    cluster: PUSHER_APP_CLUSTER,\n    authorizer: PusherBatchAuthorizer,\n    authDelay: 200, // Optional: delay in milliseconds before executing auth request\n    authEndpoint: AUTH_ENDPOINT // Ensure this points to your server-side batch auth handler\n});\n\n// Subscribe to multiple private and presence channels\n// The PusherBatchAuthorizer will automatically batch authentication requests for these\n// into a single HTTP call to the authEndpoint.\nconst privateChannel1 = pusher.subscribe('private-chat-1');\nconst privateChannel2 = pusher.subscribe('private-messages-user-abc');\nconst presenceChannel = pusher.subscribe('presence-online-users');\n\n// Example of binding to an event on a channel\nprivateChannel1.bind('client-message', (data: any) => {\n    console.log('Received message on private-chat-1:', data);\n});\n\npresenceChannel.bind('pusher:member_added', (member: any) => {\n    console.log(`Member added to presence-online-users: ${member.id}`);\n});\n\nconsole.log('Pusher client initialized and channels subscribed. Authentication requests will be batched.');\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}