Skip to content
_CORE
AI & Agentic Systems Core Information Systems Cloud & Platform Engineering Data Platform & Integration Security & Compliance QA, Testing & Observability IoT, Automation & Robotics Mobile & Digital Banking & Finance Insurance Public Administration Defense & Security Healthcare Energy & Utilities Telco & Media Manufacturing Logistics & E-commerce Retail & Loyalty
References Technologies Blog Know-how Tools
About Collaboration Careers
CS EN DE
Let's talk

The Complete Guide to WebSocket

18. 10. 2023 1 min read intermediate

WebSocket enables bidirectional real-time communication between client and server.

HTTP vs WebSocket

  • HTTP — request/response, stateless, polling
  • WebSocket — persistent connection, bidirectional, real-time

Handshake

GET /ws HTTP/1.1
Upgrade: websocket
Connection: Upgrade

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade

Server (Node.js)

import { WebSocketServer } from “ws”;

const wss = new WebSocketServer({ port: 8080 });
wss.on(“connection”, (ws) => {
ws.on(“message”, (data) => {
// broadcast to all
wss.clients.forEach((client) => client.send(data));
});
});

Client

const ws = new WebSocket(“ws://localhost:8080”);
ws.onmessage = (event) => console.log(event.data);
ws.send(“Hello”);

Use cases

  • Chat applications
  • Live notifications
  • Real-time dashboards
  • Collaborative editing
  • Gaming
  • Trading/finance feeds

Scaling

WebSocket holds a persistent connection. For scaling: Redis pub/sub for coordination between servers. Or Socket.io with Redis adapter.

Alternatives

  • SSE (Server-Sent Events) — unidirectional (server → client), simpler
  • Long polling — fallback for older clients
  • gRPC streaming — for service-to-service

When to Use WebSocket

Real-time bidirectional data. For unidirectional streams (notifications, feeds) consider SSE — simpler.

websocketreal-timebackend
Share:

CORE SYSTEMS team

We build core systems and AI agents that keep operations running. 15 years of experience with enterprise IT.