Scalable Real-time Communication With Pusher

What and why?

Pusher is a hosted API service which makes adding real-time data and functionality to web and mobile applications seamless.

Pusher works as a real-time communication layer between the server and the client. It maintains persistent connections at the client using WebSockets, as and when new data is added to your server. If a server wants to push new data to clients, they can do it instantly using Pusher. It is highly flexible, scalable, and easy to integrate. Pusher has exposed over 40+ SDKs that support almost all tech stacks.

In the context of delivering real-time data, there are other hosted and self-hosted services available. It depends on the use case of what exactly one needs, like if you need to broadcast data across all the users or something more complex having specific target groups. In our use case, Pusher was well-suited, as the decision was based on the easy usage, scalability, private and public channels, webhooks, and event-based automation. Other options which we considered were Socket.IO, Firebase & Ably, etc.

Pusher is categorically well-suited for communication and collaboration features using WebSockets. The key difference with Pusher: it’s a hosted service/API. It takes less work to get started, compared to others, where you need to manage the deployment yourself. Once we do the setup, it comes to scaling, that reduces future efforts/work.

Some of the most common use cases of Pusher are:

1. Notification: Pusher can inform users if there is any relevant change. Notifications can also be thought of as a form of signaling, where there is no representation of the notification in the UI. Still, it triggers a reaction within an application.

2. Activity streams: Stream of activities which are published when something changes on the server or someone publishes it across all channels.

3. Live Data Visualizations: Pusher allows you to broadcast continuously changing data when needed.

4. Chats: You can use Pusher for peer to peer or peer to multichannel communication.

In this blog, we will be focusing on using Channels, which is an alias for Pub/Sub messaging API for a JavaScript-based application. Pusher also comes with Chatkit and Beams (Push Notification) SDK/APIs.

Step 1: Getting Started

Setup your account on the Pusher dashboard and get your free API keys.

Pusher Dashboard
Pusher Dashboard

Image Source: Pusher

Add Pusher to your project:

or using npm

Step 2: Subscribing to Channels

There are three types of channels in Pusher: Public, Private, and Presence.

In your application, you can create a subscription and start listening to events on:

Step 3: Creating Channels

For creating channels, you can use the dashboard or integrate it with your server. For more details on how to integrate Pusher with your server, you can read ( Server API). You need to create an app on your Pusher dashboard and can use it to further trigger events to your app.

Or

Integrate Pusher with your server. Here is a sample snippet from our node app:

Step 4: Adding Security

As a default behavior, anyone who knows your public app key can open a connection to your channels app. This behavior does not add any security risk, as connections can only access data on channels.

For more advanced use cases, you need to use the “Authorized Connections” feature. It authorizes every single connection to your channels, and hence, avoids unwanted/unauthorized connection. To enable the authorization, set up an auth endpoint, then modify your client code to look like this.

For more details on how to create an auth endpoint for your server, read this. Here is a snippet from Node.js app

Step 5: Scale as you gro

Pusher comes with a wide range of plans which you can subscribe to based on your usage. You can scale your application as it grows. Here is a snippet from available plans for mode details you can refer this.

Pusher pricing plans
Pusher pricing plans

Conclusion

This article has covered a brief description of Pusher, its use cases, and how you can use it to build a scalable real-time application. Using Pusher may vary based on different use cases; it is no real debate on what one can choose. Pusher approach is simple and API based. It enables developers to add real-time functionality to any application in very little time.

Originally published at https://www.velotio.com.

Velotio Technologies is an outsourced software and product development partner for technology startups & enterprises. #Cloud #DevOps #ML #UI #DataEngineering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store