How to use Pusher Channels with Adonis
You will need an Adonis project to work with. If you don't have one, follow the GitHub link to clone the example repo.
Introduction
In this tutorial, I’ll show you how to integrate Pusher Channels into an existing Adonis application of yours. In my opinion Adonis is a must-have tool for a dev for its simplicity and efficiency but coupled with the power of Pusher Channels, the combo could not be more astonishing 😍. You can really come up with very awesome apps.
Prerequisites
This tutorial assumes you have an existing Adonis project already set up with all the required dependencies. I’ll guide you through the steps involved in integrating Pusher Channels to your project.
Set up Pusher
Head over to Pusher and create your account or sign in if you already have a account.
Next, you should create a new Pusher app instance. This registration provides credentials which can be used to communicate with the created Pusher instance. Copy the App ID, Key, Secret, and Cluster from the App Keys section and put them in the .env
file located at your project root:
//.env
PUSHER_APP_KEY=<APP_KEY>
PUSHER_APP_SECRET=<APP_SECRET>
PUSHER_APP_ID=<APP_ID>
PUSHER_APP_CLUSTER=<APP_CLUSTER>
We’ll use those keys further in this tutorial to link Pusher with our Adonis project.
If you complete this step you’ll now need to install the Pusher SDK as well as other necessary dependencies.
We won’t use the Pusher SDK directly but instead use a Pusher provider for Adonis. This provider acts as an interface between the Pusher SDK and the Adonis.js ecosystem.
But we should first install the Pusher SDK by running this command:
#if you want to use npm
npm install pusher
#or if you prefer Yarn
yarn add pusher
Now, you can install the Pusher provider for Adonis with this command:
#if you want to use npm
npm install adonis-pusher
#or if you prefer Yarn
yarn add adonis-pusher
Last you will need to add the provider to AdonisJS at start/app.js
:
const providers = [
...
'adonis-pusher/providers/Pusher'
]
How to emit events
This section will focus on emitting server-side events with data. With Pusher Channels, you can open a bidirectionnal connection between your browser or client and your backend in such a way that they can communicate easily and with no interruption. So all data sent to this open channel is received by the other part involved in this communication. Let’s see how we can implement this in practice.
First we have to fire events with Adonis.js and then tell what to do when those events are fired.
Event.fire('new::event', data)
This simple line of code tells your Adonis app to emit/fire the new::event
event and with it any data you want to send throughout your Adonis channel, as it happens data
. You can fire your event from any controller function of your project provided you define your event name and the data you want it to carry 🙂 .
But the event you submit above doesn’t have any listener, so you have to define a listener for it. Inside the listener we’ll trigger a new channel from our backend every time our new::event
is emit. That’s the mechanism.
Emit events with Pusher Channels
Create a file named event.js
in the start
directory of your project. In this file, we’ll define our event that will be fired every time we need to send a message via Pusher Channels.
//events.js
const Pusher = use('Pusher')
const Event = use('Event');
const Env = use('Env');
// set up Pusher
let pusher = new Pusher({
appId: Env.get('PUSHER_APP_ID'),
key: Env.get('PUSHER_APP_KEY'),
secret: Env.get('PUSHER_APP_SECRET'),
cluster: Env.get('PUSHER_APP_CLUSTER'),
encrypted: false
});
//fire new event
Event.when('new::event', async (comment) => {
pusher.trigger('new-channel', 'pusher-event', {
comment
})
});
First, we need to pull in the Event
, Pusher
(using the adonis-pusher package we installed earlier) and Env
service providers.
Next, we registered a listener for the new::event
event, after which we initialize and configure Pusher.
When we are done with the Pusher configuration, we trigger a pusher-event
event on our new-channel
with the trigger
method. The new-channel
(a Pusher channel) as I said above is an open bidirectionnal connection between your browser(client) and your backend to enable realtime exchanges of data. Great isn’t it? 😎
Subscribe to your Pusher channel
From your client, you can simply subscribe to your channels with the following block of code:
//main.js for example
let pusher = new Pusher(`YOUR_PUSHER_APP_ID`, {
cluster: `YOUR_PUSHER_CLUSTER`,
encrypted: false
});
//Subscribe to the channel we specified in our Adonis Application
let channel = pusher.subscribe('new-channel')
channel.bind('pusher-event', (data) => {
//do here any process you want with the data retrieved
})
First we declare a new instance of Pusher using our credentials. Then we simply subscribe to our channel we defined from our backend, and last we can listen to our pusher-event
.
Conclusion
We’re done for this tutorial. I do hope you’ve understood how you can integrate Pusher Channels to your Adonis project. If you enjoy the lesson, I think you can build great apps with this nice combo. You can take a look at the Github repo if you want, it depicts a real word use case of what we’ve seen in this tutorial.
13 March 2019
by Ethiel Adiassa