0
0
Computer Networksknowledge~3 mins

Why WebSockets for real-time communication in Computer Networks? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your online chat could feel as instant as talking face-to-face?

The Scenario

Imagine you are chatting with a friend online, but every time you send a message, you have to refresh the page to see if they replied. This makes conversations slow and frustrating.

The Problem

Using traditional methods like repeatedly asking the server if there is a new message wastes time and internet data. It also causes delays and can miss messages, making real-time chat or live updates nearly impossible.

The Solution

WebSockets create a direct, always-open connection between your device and the server. This lets messages and updates flow instantly both ways without waiting or refreshing, making real-time communication smooth and fast.

Before vs After
Before
setInterval(() => fetch('/check-messages'), 5000);
After
const socket = new WebSocket('wss://server.com'); socket.onmessage = e => displayMessage(e.data);
What It Enables

WebSockets enable instant, two-way communication that powers live chats, online games, and real-time notifications seamlessly.

Real Life Example

When you use a live sports app that updates scores the moment a goal is scored, WebSockets are working behind the scenes to deliver that instant update.

Key Takeaways

Manual checking for updates is slow and inefficient.

WebSockets keep a constant connection for instant data exchange.

This technology makes real-time apps like chats and live feeds possible.