Introduction
I recently discovered Pusher that provides real time communication amongst different processes whether it be server to server or server to client.
I followed this tutorial for a MERN stack based messaging app I am currently working on. With just a few lines of code after setting up the Pusher project, I was able to receive updates of my MongoDb instance on my frontend.
Basics
Pusher allows you to send and receive events from channels.
Server Side
From the server side, you trigger events based on the event names on a particular channel such as the following:
The library to be used on the server can be installed by the following command:
npm i pusher
Client Side
The client side, you subscribe to particular channels and bind to events both based on the respective names such as the following useEffect
:
The library to be used on the client side can be installed by the following command:
npm i pusher-js
Conclusion
To summarize in the context of MERN apps, in your server, you observe changes to your data from MongoDB and send those updates on particular channels and with specific event names. And on your front end, you subscribe to the channel and bind to the event names for your real time updates.
The setup was smooth and the free tier benefits are definitely generous with great documentation.
I'd be interested to hear about your experience with other similar technologies or any other information about Pusher.
Top comments (1)
Good