This guide will take you through integrating deepstreamHub's events with Pusher's event system. If you'd like to dive right into the code you can have a look at the GitHub repository for this tutorial here.
Whether you have legacy components using Pusher and you're moving towards deepstreamHub, or you want the additional features deepstreamHub provides on top of an event system, integrating deepstreamHub and forwarding events is easy. We'll be using the deepstreamHub JavaScript client SDK, the Pusher client SDK for JavaScript and the Pusher server SDK for JavaScript.
Create a free account and get your API key
Connecting to deepstreamHub
Getting started and logging into deepstreamHub is easy with the client SDKs. We provide a few different methods of authentication, such as email and HTTP webhook authentication. This means that we can easily get started with sending and receiving (publishing and subscribing, if you will) data between clients. In this tutorial we'll just be using open authentication, which means we can login without credentials, i.e. client.login()
.
However to follow along with the Pusher side of this tutorial, you'll need to set up a small web server to authenticate users. If you'd like to take a look at this, have a look at the repository code.
Forwarding an event from Pusher
The Pusher API for publishing events differs between the client and server; with deepstreamHub it's the same on both so we only need to write a little bit of code. A common example would be where a Pusher client is sending notifications to other clients and we want to notify the deepstreamHub clients as well.
To do this we'll just need a simple back-end process that converts the Pusher events to deepstreamHub events. In Pusher, events from clients need to be prefixed with client-
, so we'll bind to the client-notification
topic and whenever we receive a message on that topic, forward it to our deepstreamHub clients.
const Pusher = require('pusher-js/node')
const deepstream = require('deepstream.io-client-js')
client.login()
const pusher = new Pusher(config.key, { ... })
const channel = pusher.subscribe('private-my-channel')
channel.bind('client-notification', (data) => {
client.event.emit('notification', data)
})
Now any deepstreamHub client that is subscribed to the notification
topic as follows is able to receive events from Pusher.
const deepstream = require('deepstream.io-client-js')
client.login()
client.event.subscribe('notification', (data) => {
// do something with data
})
We could take this even further as well and ignore the channel the message comes from. By binding to the Pusher
object itself, on any client-notification
event we can forward it to deepstreamHub clients as follows.
pusher.bind('client-notification', (data) => {
client.event.emit('notification', data)
})
Sending deepstreamHub events alongside Pusher
Another common use case is to have some back end process sending events to interested clients periodically. While you're migrating the Pusher clients to deepstreamHub, you can easily start sending the events via deepstreamHub as follows.
const deepstream = require('deepstream.io-client-js')
const Pusher = require('pusher')
const client = deepstream('<Your app URL>')
client.login()
const pusher = new Pusher({ ... })
// when something happens
client.event.emit('notification', data)
pusher.trigger('private-my-channel', 'client-notification', data)
And then as above, any clients (back end processes, browser based users or IoT devices) can be informed of the event as we demonstrated earlier.
client.event.subscribe('notification', (data) => {
// do something with data
})
And that's it. Thanks for sticking with me. Be sure to check out the other guides and integrations we have, or check out building something more substantial with deepstreamHub via an example app.