Skip to content

What Are WebSockets? Definition and Examples

Quick answer

WebSockets keep a two-way channel open between browser and server so either side can send at any time. Learn the handshake, polling versus push, and a runnable echo example.

What Are WebSockets? Definition and Examples

WebSockets are a web protocol that opens a two-way channel between a browser and a server, so either side can send data at any moment. Unlike a normal HTTP request, which is a question followed by an answer, a WebSocket stays open, which suits chat, live scores and dashboards.

Key takeaways

  • A WebSocket is a two-way channel: client and server can both send whenever they like.
  • It begins as an HTTP request that asks to upgrade, then switches protocol.
  • Polling repeats the question; a WebSocket lets the server speak when something changes.
  • Use it when both sides send often. Use plain HTTP when updates are rare.

On this page:

ApproachWho starts each messageConnectionGood for
Plain HTTP requestClient onlyOne request, one responseLoading pages, submitting forms
PollingClient, repeatedly on a timerMany separate requestsRare updates, simple setups
WebSocketEither side, at any timeOne open channelChat, live scores, shared dashboards
WebhookOne server calls another serverA single request per eventServer-to-server notifications

What are WebSockets?

They are a standard way for a browser and server to keep a conversation going. The IETF's RFC 6455 (December 2011) says the WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment and a remote host. It describes a two-way channel where each side can, independently of the other, send data at will.

MDN Web Docs (last modified 12 November 2026) puts the benefit in developer terms: the WebSocket API lets you send messages to a server and receive responses without having to poll the server for a reply.

How does a WebSocket connection work?

It starts as an ordinary HTTP request. RFC 6455 says the protocol's only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. After the handshake, the connection stops behaving like request and response.

Illustrative trace of a chat app.

  1. The browser sends an HTTP request with Upgrade headers asking to switch to WebSocket.
  2. The server accepts and replies with status 101 Switching Protocols.
  3. Either side now sends messages whenever it wants. The server can push a reply with no new request from the browser.

To try it, run this small server with Node.js after installing the ws package (npm install ws). It echoes every message back.

// server.js
const { WebSocketServer } = require("ws");
const wss = new WebSocketServer({ port: 8080 });
wss.on("connection", (ws) => {
  ws.on("message", (data) => ws.send(`echo: ${data}`));
});

Then, while the server runs, open a blank tab (about:blank), open the browser console and paste this. A blank tab avoids sites whose security policy blocks outside connections.

const socket = new WebSocket("ws://localhost:8080");
socket.onopen = () => socket.send("hello");
socket.onmessage = (event) => console.log("received:", event.data);

You should see "received: echo: hello".

Illustrative count (made-up polling interval). If a live-score page polls every 5 seconds, each open page sends 12 requests a minute, even when the score has not moved. With a WebSocket, the server sends a message only when the score changes.

Where are WebSockets used?

They fit anything where updates arrive unpredictably and both sides may talk: chat, multiplayer games, shared documents and live trading views. A made-up Indian example is a live cricket score ticker, or a doubt-solving chat where the teacher's reply should appear without a refresh.

Decision rule. Use WebSockets when both sides send often. Use plain HTTP or polling when updates are rare, because they are simpler to build and host.

What are the common mistakes and confusions?

  • Thinking WebSocket replaces HTTP. It starts with HTTP and is used alongside it for pages and APIs.
  • Using it for rare updates. An open connection per user takes server resources, so a plain request is cheaper when little changes.
  • Skipping encryption. Use wss:// (WebSocket over TLS) on public sites, as you would use https://.
  • Ignoring reconnects. Networks drop. Real apps need code to reconnect and recover missed messages.

Compare it with a request-style interface in what an API is. For the client and server split, read frontend and backend development differences. If you build interfaces in JavaScript, see frontend web development with React and a beginner's guide to JavaScript frameworks.

Next step

Build the echo example, then extend it so a message from one browser tab reaches every connected tab. That is a basic chat room. For the wider path, follow the backend developer roadmap.

Frequently asked questions

What are WebSockets in simple words?

A WebSocket is an open two-way line between a browser and a server. Once connected, either side can send a message at any time without a new request.

How is WebSocket different from HTTP?

HTTP is request and response: the client asks, the server answers. A WebSocket starts with an HTTP upgrade request, then stays open so both sides can send data whenever they want.

What is the difference between WebSockets and polling?

Polling asks the server again and again on a timer, even if nothing changed. With a WebSocket, the server pushes a message only when there is something new to send.

Where are WebSockets used?

Chat, multiplayer games, live score tickers, shared documents and live dashboards. They suit cases where updates are frequent and unpredictable and either side may need to speak.

Are WebSockets secure?

They can be. Use wss://, which runs WebSocket over TLS, just as https:// secures HTTP. You still need to authenticate users and validate every message your server receives.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.