Create an instance of a Pusher Channels client that subscribes and reacts to events on the appropriate channel. Additionally, send data to your Serverless Function that will trigger a push event.

Create a main.js file where you will initialize a Channels object with your app-key, subscribe to the appropriate channel and bind a callback function to react to events within that channel.

// Initialize Channels clientlet channels =newPusher('app-key',{
cluster:'cluster-region'})// Subscribe to the appropriate channellet channel = channels.subscribe('channel-name')// Bind a callback function to an event within the subscribed channel
channel.bind('event-name',function(data){// Do what you wish with the data from the event})

Initializing a Channels client, subscribing to a channel, and binding to an event.

All that's remaining on the client is to create a way to send data to your Serverless Function to trigger push events. To achieve this, add the snippet below to your main.js file.

An example now.json file that provides the app with environment variables.

Add the dependencies for the Serverless Function from inside the /api directory.

cd api && npm i pusher

Entering the /api directory and installing the pusher dependency.

Create a channels-event.js file inside the /api directory that initializes a new Channels object and receives data from the req.body helper method, before invoking channels.trigger to register the event.

If you have not yet installed Now, you can do so by installing Now CLI. You are now ready to deploy your app with a single command.

now

Deploying your project with the now command.

If you want to deploy your Channels project when you push to a Git repository, you can use either Now for GitHub or Now for GitLab to have your project automatically deployed on every push, and aliased on push to master.