Real-time applications are becoming a significant part of modern web development, offering dynamic experiences for users by delivering instant updates without needing to refresh pages. Whether it’s collaborative tools, chat apps, live data dashboards, or online gaming, real-time features bring a fluid user experience.
In the context of MERN (MongoDB, Express, React, and Node.js) stack, implementing real-time features can be achieved by integrating WebSockets or GraphQL. This article will cover the technologies, their advantages, and how to implement them in MERN applications. The content is especially valuable for developers ranging from freshers to those with up to five years of experience.
Understanding Real-Time Updates
Real-time web applications provide users with updates instantly as changes occur, making them particularly useful in scenarios where quick responses are critical. For example, a live chat application shows incoming messages immediately without requiring the user to refresh the page. This is accomplished using bidirectional communication between the client and server, which allows the server to push updates to the client.
WebSockets: The Core of Real-Time Communication
What Are WebSockets?
WebSockets provide a persistent connection between the client and server, allowing messages to be transmitted in both directions at any time. Unlike HTTP, where communication is initiated by the client with each request, WebSockets establish a full-duplex communication channel. This makes WebSockets ideal for real-time features, as updates can be pushed from the server to the client without any delays.
Advantages of WebSockets in MERN Applications:
- Low Latency: Real-time updates are delivered with minimal delay.
- Efficient Use of Resources: WebSockets maintain a single connection, reducing overhead compared to constantly polling the server.
- Scalability: WebSockets can handle large-scale real-time applications, making them suitable for enterprise solutions.
Integrating WebSockets with MERN Stack
Let’s dive into how you can add WebSockets to a MERN application to create a real-time feature, such as a live chat.
Step 1: Setting Up the Backend (Node.js with Express)
To integrate WebSockets in Node.js, the ws library is commonly used. Begin by installing it:

Next, create a WebSocket server in your Node.js application:

In this setup, every time a client connects, the server establishes a WebSocket connection. When a message is sent by any client, it is broadcast to all other connected clients.
Step 2: Setting Up the Frontend (React)
On the React side, use the WebSocket API to connect to the server and handle messages:


GraphQL Subscriptions for Real-Time Updates
What Are GraphQL Subscriptions? GraphQL is a query language for APIs, and it’s widely used in MERN applications. While WebSockets handle real-time updates with low latency, GraphQL introduces subscriptions as a mechanism to listen for real-time data. Subscriptions in GraphQL allow the server to push updates to clients when certain events happen. Advantages of GraphQL Subscriptions:- Structured Queries: Clients can specify exactly what data they want to receive in real-time.
- Better Integration: Works well with an existing GraphQL API.
- Efficient: Reduces unnecessary data transfer by delivering only the relevant data to clients.
Implementing GraphQL Subscriptions in a MERN App
To implement GraphQL subscriptions in a MERN application, use Apollo Server and subscriptions-transport-ws. Install the necessary dependencies:
Set up the server with WebSocket support for GraphQL subscriptions:


On the client-side, Apollo Client provides the necessary tools for working with subscriptions. Use graphql-ws to enable WebSocket communication:

Conclusion
Real-time features are increasingly crucial in modern web applications. With WebSockets providing low-latency bidirectional communication and GraphQL subscriptions enabling structured real-time queries, MERN developers have the tools to create dynamic, engaging applications. These technologies open the door to real-time chat applications, live data updates, and much more, making them essential skills for developers at all levels.