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,instead, 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.
- Chatkit is designed to make chat integration to your app as simple as possible. It allows you to add group chat and 1 to 1 chat feature to your app. It also allows you to add file attachments and online indicators.
- Beams are used for adding Push Notification in your Mobile App. It includes SDKs to seamlessly manage push token amd send notifications.
Step 1: Getting Started
Setup your account on the Pusher dashboard and get your free API keys.
Image Source: Pusher
- Click on Channels
- Create an App. Add details based on the project and the environment.
- Click on the App Keys tab to get the app keys.
- You can also check the getting started page. It will give code snippets to get you started.
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.
- Public channels: These channels are public in nature, and so anyone who knows the channel name can subscribe to the channel and start receiving messages from the channel. Public channels are commonly used to broadcast general/public information, which does not contain any secure information or user-specific data.
- Private channels: These Channels have an access control mechanism that allows the server to control who can subscribe to the channel and receive data from the channel. All private channels should have a private- prefixed to the name. They are commonly used where sever needs to know who can subscribe to the channel and validate the subscribers.
- Presence channels: It is an extension to the private channel. In addition to the properties which private channels have, it lets the server 'register' users information on subscription to the channel. It also enables other members to identify who is online.
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. You can use it to further trigger events to your app.
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 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 grow
Pusher comes with a wider 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.
Image Source: Pusher
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 once can choose. Pusher approach is simple and API based, which enables developers to add real-time functionality to any application in very little time.
If you want to get hands-on tutorials/blogs, please visit here.