Skip to content

WebSocket

In one line

WebSocket is a communication protocol that provides a persistent, bidirectional connection between a browser and a server.

In simple words

WebSocket is a protocol that allows a browser and server to maintain an open, bidirectional connection. Unlike HTTP (where the client must request each time), WebSocket lets either side send messages at any time — ideal for real-time applications.

WebSocket starts as an HTTP request that "upgrades" to a WebSocket connection. Once established, the connection stays open, and both client and server can send messages whenever they want. This avoids the overhead of repeated HTTP requests and enables true real-time communication.

Common use cases include chat applications, live sports updates, collaborative editing, multiplayer games, and real-time dashboards. The browser API is simple: const ws = new WebSocket('wss://...'), then listen for onmessage events and call ws.send() to send data.

Example

javascript
const ws = new WebSocket("wss://chat.example.com");
ws.onmessage = (event) => {
  console.log("Received:", event.data);
};
ws.send("Hello server!");

A WebSocket connection that receives messages and sends one. The connection stays open for real-time communication.

Related terms

Related Resources

Related Lessons

  • HTTP

    HTTP and related protocols.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary