WebSockets are used for real time communication between client and server ๐
In simple words
WebSockets keep a persistent connection open
So data can flow instantly both ways ๐
Without WebSockets โ
โข Constant polling
โข Delayed updates
โข Poor real time experience
๐ง What WebSockets Are Used For
โข Chat applications ๐ฌ
โข Live notifications ๐
โข Online gaming ๐ฎ
โข Live dashboards ๐
โข Stock price updates ๐
๐ Real World Examples
โข WhatsApp live chat
โข Live comments on streams
โข Multiplayer games
โข Real time tracking apps
๐ How WebSockets Work
Client opens connection โก
Server accepts connection ๐ฅ๏ธ
Both send and receive data anytime ๐
Connection stays open ๐ก
๐ป Example: Simple WebSocket using JavaScript
const socket = new WebSocket("ws://localhost:3000");
socket.onopen = () => {
socket.send("Hello Server");
};
socket.onmessage = (event) => {
console.log(event.data);
};