WebSocket API and protocol explained: How they work, are used and more

Each string should represent a sub-protocol name and server accepts only one of passed sub-protocols in the array. Accepted sub-protocol can be determined by accessing protocol property of WebSocket object. The primary interface for connecting to a WebSocket server and then sending and receiving data on the connection. In general, WebSocket connections can stay open indefinitely as long as both the client and server remain connected and the network is stable. You can think of a WebSocket connection as a long-lived, bidirectional, full-duplex communication channel between a web client and a web server.

how does websocket work

And for binary data, we can choose between Blob and ArrayBuffer formats. In the browser, we directly work only with text or binary frames. The server should respond with a list of protocols and extensions that it agrees to use.

What is a WebSocket connection?

WebSockets can be created by upgrading HTTP or HTTPS connections but they can also be created directly using other methods. For example, the following line of JavaScript code will create a WebSocket. The server will respond using the Sec-WebSocket-Protocol header to confirm which subprotocol will be used. The Sec-Websocket-Accept header is included with the initial response from the server.

  • Uncover how WebSockets enable bi-directional communication between servers and clients, and their applications.
  • WebSocket uses HTTP as
    the initial transport mechanism, but keeps the TCP connection alive after
    the HTTP response is received so that it can be used for sending messages
    between client and server.
  • The connection is being terminated because the endpoint received data of a type it cannot handle (e.g., a text-only endpoint receiving binary data).
  • That’s set by socket.binaryType property, it’s “blob” by default, so binary data comes as Blob objects.
  • Upon receiving a close frame, an endpoint (client or server) has to send a close frame as a response (echoing the status code received).

A WebSocket server can receive events from clients, process them to update the
application state, and synchronize the resulting state across clients. Here’s how to adapt the server example to provide secure connections. See the
documentation of the ssl module for configuring the context securely. WebSocket is a modern way to have persistent browser-server connections.

The problem: Low latency client-server and server-client connections

The message event is fired when data is received through a WebSocket. Messages might contain string (plain text) or binary data, and it’s up to you how that data will be processed and visualized. After a successful opening handshake, the client and the server can use the WebSocket connection to exchange messages in full-duplex mode.

how does websocket work

Text frames carry human-readable messages; binary frames can carry any type of binary data, and control frames are used for managing the connection, such as opening, closing, and error handling. Think of WebSockets as a conversation that doesn’t end after one message. You can easily exchange messages between the browser and server without constant requests. This is particularly useful for applications which often require updates, like chat apps, online games, or live financial data feeds.

Closing the connection

The Sec-WebSocket-Protocol header is part of the WebSocket handshake. It is sent by the client to ask the server to use a specific subprotocol. how does websocket work This header can appear multiple times within the request, or instead, multiple subprotocols can be listed in a comma-delimited format.

how does websocket work

The client is terminating the connection because the server failed to negotiate an extension during the opening handshake. Used by apps and the WebSocket API to indicate that no status code was received, although one was expected. Connection close frame; leads to the connection being terminated.

Opening handshake

This message typically includes an HTTP 101 (Switching Protocols) status code. Uncover how WebSockets enable bi-directional communication between servers and clients, and their applications. This guide highlights why WebSockets are superior in certain scenarios compared to traditional HTTP, vital for web developers and IT professionals. On the server side, websockets executes the handler coroutine hello
once for each WebSocket connection. Being a modern protocol, cross origin communication is baked right into WebSocket.

But the user has a slow network connection, maybe on a mobile internet, outside of a city. Here the server responds that it supports the extension “deflate-frame”, and only SOAP of the requested subprotocols. For demo purposes, there’s a small server server.js written in Node.js, for the example above, running.

Establishing WebSocket via HTTP/1.1

The server will respond with the Sec-WebSocket-Extensions header to confirm what protocol-level extensions will be used for the remainder of the connection. The server will not include more than one instance of this header in the response. The Sec-WebSocket-Extensions header is part of the WebSocket handshake. It is sent by the client to ask the server to use a specific set of extensions. This header can be included more than one time or a common-delimited list of extensions can be included on a single line. The extensions must be chosen from the IANA WebSocket Extension Name Registry.

To close a WebSocket connection, a closing frame is sent (opcode 0x08). In addition to the opcode, the close frame may contain a body that
indicates the reason for closing. If either side of a connection receives
a close frame, it must send a close frame in response, and no more data
should be sent over the connection. Once the close frame has been received
by both parties, the TCP connection is torn down.

WebSockets provide a bidirectional, full-duplex communications channel
that operates over HTTP through a single TCP/IP socket connection. At its
core, the WebSocket protocol facilitates message passing between a client
and server. This article provides an introduction to the WebSocket
protocol, including what problem WebSockets solve, and an overview of how
WebSockets are described at the protocol level.

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.