Install the SDK

To install the SDK, you can use npm or yarn. Run the following command:

npm install pusher-js

Initialize the SDK

import Pusher from 'pusher-js';

const pusher = new Pusher('YOUR_APP_KEY', {
    wsHost: 'ws.pulses.cloud',
    encrypted: true,
    disableStats: true,
    enabledTransports: ['ws', 'wss'],
    cluster: 'pulses'
});

Subscribe to a channel

To subscribe to a channel, you can use the pusher.subscribe method. Run the following command:

const channel = pusher.subscribe('channel-name');

Bind to an event

To bind to an event, you can use the channel.bind method. Run the following command:

channel.bind('event-name', function(data) {
    console.log('Received event with data:', data);
});

Publish an event from the server

To publish an event from the server, you can use the trigger method. Run the following command:

// First, run 'npm install pusher'
const Pusher = require('pusher');

const pusher = new Pusher('YOUR_APP_KEY', {
    wsHost: 'ws.pulses.cloud',
    encrypted: true,
    disableStats: true,
    enabledTransports: ['ws', 'wss'],
    cluster: 'pulses'
});

pusher.trigger('channel-name', 'event-name', {
    message: 'Hello World'
});

If there isn’t an example in a language that you are familiar with then refer to Pusher’s Channels server libraries page to check if anyone has created one in your language.

Unsubscribe from a channel

To unsubscribe from a channel, you can use the channel.unsubscribe method. Run the following command:

channel.unsubscribe('channel-name');

Disconnect from Pusher

To disconnect from Pusher, you can use the pusher.disconnect method. Run the following command:

pusher.disconnect();