0
0
Node.jsframework~3 mins

Why WebSocket protocol concept in Node.js? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your app could talk to the server instantly, like a live conversation?

The Scenario

Imagine building a chat app where users must refresh the page to see new messages.

Every time you want to get new data, you send a request and wait for a response.

The Problem

This request-response way is slow and wastes resources because the server can't send updates on its own.

Users see delays and the app feels unresponsive.

The Solution

WebSocket creates a constant open connection between client and server.

This lets both sides send messages instantly without waiting for requests.

Before vs After
Before
setInterval(() => fetch('/messages').then(response => response.json()).then(data => console.log(data)), 5000);
After
const ws = new WebSocket('ws://server'); ws.onmessage = e => console.log(e.data);
What It Enables

Real-time, two-way communication that feels instant and smooth for users.

Real Life Example

Live sports scores updating on your screen without refreshing the page.

Key Takeaways

Manual polling is slow and inefficient.

WebSocket keeps a live connection open for instant updates.

This improves user experience with real-time data.