WebSocket
Imagine a website that doesn't just wait for you to click or refresh, but instantly pushes updates, messages, or live data right to your screen. That's the power of WebSocket, a communications protocol that revolutionized web interaction. It provides a persistent, two-way connection, transforming static pages into dynamic, real-time experiences. WebSocket creates a persistent, bidirectional communication channel between client and server, enabling real-time data exchange with minimal overhead. It initiates a connection via a special HTTP 'upgrade' handshake, then operates independently of HTTP, optimizing for continuous data flow. Widely adopted across browsers and server technologies, WebSocket is the backbone for interactive features like live chat, gaming, and collaborative tools on the modern web.
AI Summary
Imagine a website that doesn't just wait for you to click or refresh, but instantly pushes updates, messages, or live data right to your screen. That's the power of WebSocket, a communications protocol that revolutionized web interaction. It provides a persistent, two-way connection, transforming static pages into dynamic, real-time experiences.
- WebSocket creates a persistent, bidirectional communication channel between client and server, enabling real-time data exchange with minimal overhead.
- It initiates a connection via a special HTTP 'upgrade' handshake, then operates independently of HTTP, optimizing for continuous data flow.
- Widely adopted across browsers and server technologies, WebSocket is the backbone for interactive features like live chat, gaming, and collaborative tools on the modern web.
The Real-Time Web Awakens
Before WebSocket, the internet largely operated on a 'request-response' model—you ask, the server answers. But for applications needing instant updates, like chat or live sports scores, this felt sluggish. WebSocket emerged as a game-changer, offering a truly interactive, bidirectional flow of information over a single TCP connection.
Think of it as opening a dedicated phone line, rather than constantly sending letters back and forth. This persistent connection means both the client (your browser) and the server can send data to each other at any time, without waiting for a new request.
More Than Just HTTP
While WebSocket is a distinct protocol from HTTP, it cleverly uses HTTP as a stepping stone. To establish a WebSocket connection, your browser first sends a regular HTTP request, but with a special 'Upgrade' header. This tells the server, 'Hey, I want to switch to a different protocol!'
If the server agrees, it responds with a '101 Switching Protocols' status code, and voilà! The connection is upgraded from HTTP to WebSocket. This clever handshake allows WebSocket to coexist with HTTP on the standard web ports, 80 and 443, which are typically open through firewalls.
Full Duplex, Low Overhead
The persistent, two-way nature of WebSocket is known as 'full-duplex' communication. This is a huge leap from older 'polling' methods, where the client would repeatedly ask the server for updates, even if there were none. Such constant querying created a lot of unnecessary network traffic and delay.
With WebSocket, once the connection is open, messages can flow freely and instantly in both directions. This dramatically reduces overhead, as there's no need to constantly re-establish connections or send redundant headers, making real-time data transfer incredibly efficient.
Naming the New Connection
Just like HTTP uses http:// or https://, WebSocket introduced its own URI schemes: ws:// for unencrypted connections and wss:// for secure, encrypted connections. These prefixes clearly indicate that a different communication protocol is at play, ready for dynamic interaction.
From Concept to Standard
The seeds of WebSocket were planted in the HTML5 specification, initially referred to as 'TCPConnection'—a placeholder for a future TCP-based socket API for the web. Visionaries like Michael Carter and Ian Hickson championed its development, aiming to solve the inefficiencies of existing real-time hacks like Comet.
Older methods, like Comet, struggled with high overhead due to repeated TCP handshakes and HTTP headers for even small messages. The WebSocket protocol was designed to provide a lightweight, standardized alternative without compromising web security, a crucial balancing act.
The name 'WebSocket' was coined during an IRC chat, and the standard quickly gained traction. Google Chrome 4 was the first browser to ship full support in December 2009, making this powerful technology accessible to developers and users worldwide. The protocol was later standardized by the IETF as RFC 6455 in 2011.
Evolving for Efficiency
The WebSocket standard didn't stop there. Later, RFC 7692 introduced a compression extension, utilizing the DEFLATE algorithm. This means messages exchanged over WebSocket can be compressed on a per-message basis, further reducing data transfer sizes and boosting performance.
The Web API: A Developer's Tool
For web developers, interacting with WebSocket is straightforward thanks to its intuitive API. A web application can create a WebSocket object, which then handles the complex underlying protocol details.
Key methods like send() allow applications to transmit data, while event listeners like onmessage, onopen, and onclose enable them to react to incoming messages, connection establishment, or disconnection. This abstraction makes building real-time features far more manageable.
How It Works: The Protocol Unpacked
At its core, the WebSocket protocol unfolds in three main phases: an opening handshake, a continuous frame-based message exchange, and a closing handshake. This structured approach ensures reliable and efficient communication throughout the connection's lifespan.
The Opening Handshake
The initial handshake is where the magic happens. A client sends a standard HTTP GET request, but crucially includes headers like Connection: Upgrade and Upgrade: websocket. It also sends a unique, randomly generated Sec-WebSocket-Key.
The server, upon receiving this, calculates a Sec-WebSocket-Accept value based on the client's key and sends it back in a '101 Switching Protocols' HTTP response. This confirms the protocol switch and marks the successful transition from HTTP to the WebSocket protocol.
Guard Against Proxies
The Sec-WebSocket-Key and Sec-WebSocket-Accept values are not for authentication. Instead, they serve as a crucial security measure to prevent misconfigured caching proxies from inadvertently re-sending or corrupting a WebSocket conversation, ensuring the integrity of the connection.
Frame-Based Communication
Once the handshake is complete, data flows as a series of 'frames'—small, self-contained units of information. These frames can carry text or binary data, or they can be 'control frames' like Ping and Pong messages, used for checking connection health or measuring latency.
A single message can be sent in one frame, or it can be 'fragmented' across multiple frames. Fragmentation is especially useful for very large messages or when the total message length isn't known upfront, preventing the need to buffer the entire message before sending any part of it.
Widespread Adoption
Today, WebSocket is a cornerstone of the modern web, with virtually all major web browsers—including Chrome, Firefox, Edge, Safari, and Opera—offering full support. This broad compatibility means developers can confidently build real-time features that work seamlessly across different platforms.
Server-Side Support
It's not just browsers; major web servers and development frameworks have also embraced WebSocket. Technologies like Nginx, Apache HTTP Server, Microsoft IIS, lighttpd, and ASP.NET Core all provide robust support, demonstrating its integration into the core infrastructure of the internet.
Security and Proxies
Unlike typical HTTP requests, WebSocket connections aren't bound by the browser's 'same-origin policy' after the initial handshake. This flexibility, while powerful, introduces a security consideration: WebSocket servers must carefully validate the 'Origin' header during connection establishment to prevent cross-site hijacking attacks, similar to CSRF.
For sensitive data, relying solely on cookie-based authentication isn't enough. Best practices suggest using more robust authentication mechanisms like tokens to secure WebSocket connections, protecting against vulnerabilities like the 'Cable Haunt' discovered in 2020.
Navigating the Proxy Maze
The internet is filled with proxy servers and firewalls, which can sometimes interfere with connections. WebSocket's initial HTTP handshake helps it traverse these obstacles, especially when using standard HTTP/HTTPS ports (80 and 443).
When browsers are configured to use an explicit proxy, wss:// (secure WebSocket) connections often fare better. The underlying Transport Layer Security (TLS) handshake initiates an HTTP CONNECT method, creating a secure tunnel right through the proxy, ensuring end-to-end communication.
Learning from the Past
Early drafts of the WebSocket protocol faced challenges with reverse proxies. Some versions mistakenly included key data after HTTP headers without proper Content-Length, causing proxies to drop the data and break the connection. The introduction of the Sec-WebSocket-Key header in later drafts resolved this, ensuring smoother proxy traversal and robust connections.
Article
WebSocket
WebSocket is a computer communications protocol, providing a bidirectional communication channel over a single Transmission Control Protocol (TCP) connection. The protocol was standardized by the IETF as RFC 6455 in 2011. The current specification allowing web applications to use this protocol is known as WebSockets. It is a living standard maintained by the WHATWG and a successor to The WebSocket API from the W3C.
WebSocket is distinct from HTTP used to serve most webpages. Although they are different, RFC 6455 states that WebSocket "is designed to work over HTTP ports 443 and 80 as well as to support HTTP proxies and intermediaries", making the WebSocket protocol compatible with HTTP. To achieve compatibility, the WebSocket handshake uses the HTTP Upgrade header to change from the HTTP protocol to the WebSocket protocol.
The WebSocket protocol enables full-duplex interaction between a web browser (or other client application) and a web server with lower overhead than half-duplex alternatives such as HTTP polling, facilitating real-time data transfer from and to the server. This is achieved by providing a standardized way for the server to send content to the client without being first requested by the client, and allowing messages to be exchanged while keeping the connection open. In this way, a two-way ongoing conversation can take place between the client and the server. The communications are usually done over TCP port number 443 (or 80 in the case of unsecured connections), which is beneficial for environments that block non-web Internet connections using a firewall. Additionally, WebSocket enables streams of messages on top of TCP. TCP alone deals with streams of bytes with no inherent concept of a message. Similar two-way browser–server communications have been achieved in non-standardized ways using stopgap technologies such as Comet or Adobe Flash Player.
Most browsers support the protocol, including Google Chrome, Firefox, Microsoft Edge, Internet Explorer, Safari and Opera.
The WebSocket protocol specification defines ws (WebSocket) and wss (WebSocket Secure) as two new uniform resource identifier (URI) schemes that are used for unencrypted and encrypted connections respectively. Apart from the scheme name and fragment (i.e. # is not supported), the rest of the URI components are defined to use URI generic syntax.
History
WebSocket
WebSocket was first referenced as TCPConnection in the HTML5 specification, as a placeholder for a TCP-based socket API. In June 2008, a series of discussions were led by Michael Carter that resulted in the first version of the protocol known as WebSocket. Before WebSocket, port 80 full-duplex communication was attainable using Comet channels; however, Comet implementation is nontrivial, and due to the TCP handshake and HTTP header overhead, it is inefficient for small messages. The WebSocket protocol aims to solve these problems without compromising the security assumptions of the web. The name "WebSocket" was coined by Ian Hickson and Michael Carter shortly thereafter through collaboration on the #whatwg IRC chat room, and subsequently authored for inclusion in the HTML5 specification by Ian Hickson. In December 2009, Google Chrome 4 was the first browser to ship full support for the standard, with WebSocket enabled by default. Development of the WebSocket protocol was subsequently moved from the W3C and WHATWG group to the IETF in February 2010, and authored for two revisions under Ian Hickson.
After the protocol was shipped and enabled by default in multiple browsers, the RFC 6455 was finalized under Ian Fette in December 2011.
RFC 7692 introduced compression extension to WebSocket using the DEFLATE algorithm on a per-message basis.
Web API
WebSocket
A web application (e.g. web browser) may use the WebSocket interface to maintain bidirectional communications with a WebSocket server.
WebSocket interface
<table><thead><tr><th>Type</th><th>Name</th><th>Description</th></tr></thead><tbody><tr><td>Constructor</td><td>ws = new WebSocket(url [, protocols ])</td><td>Start opening handshake. Exceptions:</td></tr><tr><td>Method</td><td>ws.send(data)</td><td>Send data message. Return: undefined. Exceptions: Note: If the data cannot be sent (e.g. because it would need to be buffered but the buffer is full), the connection is closed and the error event is fired.</td></tr><tr><td>ws.close([ code ] [, reason ])</td><td>Start closing handshake. Return: undefined. Exceptions: Note:</td></tr><tr><td>Event</td><td>ws.onopen = (event) => {} ws.addEventListener("open", (event) => {})</td><td>Opening handshake succeeded. event type is Event.</td></tr><tr><td>ws.onmessage = (event) => {} ws.addEventListener("message", (event) => {})</td><td>Data message received. event type is MessageEvent. This event is only fired if ws.readyState is OPEN.</td></tr><tr><td>ws.onclose = (event) => {} ws.addEventListener("close", (event) => {})</td><td>The underlying TCP connection closed. event type is CloseEvent. Note:</td></tr><tr><td>ws.onerror = (event) => {} ws.addEventListener("error", (event) => {})</td><td>Connection closed due to error. event type is Event.</td></tr><tr><td>Attribute</td><td>ws.binaryType (string)</td><td>Type of event.data in ws.onmessage when a binary data message is received. Initially set to "blob" (Blob object). May be changed to "arraybuffer" (ArrayBuffer object).</td></tr><tr><td>Read-only attribute</td><td>ws.url (string)</td><td>URL given to the WebSocket constructor with the following transformations:</td></tr><tr><td>ws.bufferedAmount (number)</td><td>Number of bytes of application data (UTF-8 text and binary data) that have been queued using ws.send() but not yet transmitted to the network. It resets to zero once all queued data has been sent. If the connection closes, this value will only increase, with each call to ws.send(), and never reset to zero.</td></tr><tr><td>ws.protocol (string)</td><td>Protocol accepted by the server, or an empty string if protocols was not specified in the WebSocket constructor.</td></tr><tr><td>ws.extensions (string)</td><td>Extensions accepted by the server.</td></tr><tr><td>ws.readyState (number)</td><td>Connection state. It is one of the constants below. Initially set to CONNECTING.</td></tr><tr><td>Constant</td><td>WebSocket.CONNECTING = 0</td><td>Opening handshake is currently in progress. The initial state of the connection.</td></tr><tr><td>WebSocket.OPEN = 1</td><td>Opening handshake succeeded. The client and server may send messages to each other.</td></tr><tr><td>WebSocket.CLOSING = 2</td><td>Closing handshake is currently in progress. Either ws.close() was called or a Close message was received.</td></tr><tr><td>WebSocket.CLOSED = 3</td><td>The underlying TCP connection is closed.</td></tr></tbody></table>
Protocol
WebSocket
A diagram describing a connection using WebSocket
• Opening handshake: HTTP request and HTTP response. • Frame-based message exchange: data, ping and pong messages. • Closing handshake: close message (request then echoed in response).
Opening handshake
The client sends an HTTP request (method GET, version ≥ 1.1) and the server returns an HTTP response with status code 101 (Switching Protocols) on success. HTTP and WebSocket clients can connect to a server using the same port because the opening handshake uses HTTP. Sending additional HTTP headers (that are not in the table below) is allowed. HTTP headers may be sent in any order. After the Switching Protocols HTTP response, the opening handshake is complete, the HTTP protocol stops being used, and communication switches to a binary frame-based protocol.
<table><thead><tr><th>Side</th><th>Header</th><th>Value</th><th>Mandatory</th></tr></thead><tbody><tr><td>Request</td><td>Origin</td><td>Varies</td><td>Yes (for browser clients)</td></tr><tr><td>Host</td><td>Varies</td><td>Yes</td></tr><tr><td>Sec-WebSocket-Version</td><td>13</td></tr><tr><td>Sec-WebSocket-Key</td><td>base64-encode(16 random bytes)</td></tr><tr><td>Response</td><td>Sec-WebSocket-Accept</td><td>base64-encode(SHA1(Sec-WebSocket-Key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))</td></tr><tr><td>Both</td><td>Connection</td><td>Upgrade</td></tr><tr><td>Upgrade</td><td>websocket</td></tr><tr><td>Sec-WebSocket-Protocol</td><td>The request may contain a comma-separated list of strings (ordered by preference) indicating application-level protocols (built on top of WebSocket data messages) the client wishes to use. If the client sends this header, the server response must be one of the values from the list.</td><td>No</td></tr><tr><td>Sec-WebSocket-Extensions</td><td>Used to negotiate protocol-level extensions. The client may request extensions to the WebSocket protocol by including a comma-separated list of extensions (ordered by preference). Each extension may have a parameter (e.g. foo=4). The server may accept some or all extensions requested by the client. This field may appear multiple times in the request (logically equivalent to a single occurrence containing all values) and must not appear more than once in the response.</td></tr></tbody></table>
The following Python code generates a random Sec-WebSocket-Key.
The following Python code calculates Sec-WebSocket-Accept using Sec-WebSocket-Key from the example request above.
Sec-WebSocket-Key and Sec-WebSocket-Accept are intended to prevent a caching proxy from re-sending a previous WebSocket conversation, and does not provide any authentication, privacy, or integrity.
Though some servers accept a short Sec-WebSocket-Key, many modern servers will reject the request with error "invalid Sec-WebSocket-Key header".
Frame-based message
After the opening handshake, the client and server can, at any time, send data messages (text or binary) and control messages (Close, Ping, Pong) to each other. A message is composed of one frame if unfragmented or at least two frames if fragmented.
Fragmentation splits a message into two or more frames. It enables sending messages with initial data available but complete length unknown. Without fragmentation, the whole message must be sent in one frame, so the complete length is needed before the first byte can be sent, which requires a buffer. It was proposed to extend this feature to enable multiplexing several streams simultaneously (e.g. to avoid monopolizing a socket for a single large payload), but the protocol extension was never accepted.
• An unfragmented message consists of one frame with FIN = 1 and opcode ≠ 0. • A fragmented message consists of one frame with FIN = 0 and opcode ≠ 0, followed by zero or more frames with FIN = 0 and opcode = 0, and terminated by one frame with FIN = 1 and opcode = 0.
Frame structure
<table><thead><tr><th>Offset (bits)</th><th>Field</th><th>Size (bits)</th><th>Description</th></tr></thead><tbody><tr><td>0</td><td>FIN</td><td>1</td><td></td></tr><tr><td>1</td><td>RSV1</td><td>1</td><td>Reserved. Must be 0 unless defined by an extension. If a non-zero value is received and none of the negotiated extensions defines the meaning of such a non-zero value, the connection must be closed.</td></tr><tr><td>2</td><td>RSV2</td><td>1</td></tr><tr><td>3</td><td>RSV3</td><td>1</td></tr><tr><td>4</td><td>Opcode</td><td>4</td><td>See opcodes below.</td></tr><tr><td>8</td><td>Masked</td><td>1</td><td>See client-to-server masking below.</td></tr><tr><td>9</td><td>Payload length</td><td>7, 7+16 or 7+64</td><td>Length of the payload (extension data + application data) in bytes. Endianness is big-endian. Signedness is unsigned. The minimum number of bits must be used to encode the length.</td></tr><tr><td>Varies</td><td>Masking key</td><td>0 or 32</td><td>Random nonce. Present if the masked field is 1. The client generates a masking key for every masked frame.</td></tr><tr><td>Payload</td><td>Extension data</td><td>Payload length (bytes)</td><td>Must be empty unless defined by an extension.</td></tr><tr><td>Application data</td><td>Depends on the opcode</td></tr></tbody></table>
Opcodes
<table><thead><tr><th>Frame type</th><th>Opcode</th><th>Related Web API</th><th>Description</th><th>Purpose</th><th>Fragmentable</th><th>Max. payload length (bytes)</th></tr></thead><tbody><tr><td>Continuation frame</td><td>0</td><td></td><td>Non-first frame of a fragmented message.</td><td>Message fragmentation</td><td></td><td>263 − 1</td></tr><tr><td>Non-control frame</td><td>Text</td><td>1</td><td>send(), onmessage</td><td>UTF-8-encoded text.</td><td>Data message</td><td>Yes</td></tr><tr><td>Binary</td><td>2</td><td>Binary data.</td></tr><tr><td></td><td>3–7</td><td></td><td>Reserved for further non-control frames. May be defined by an extension.</td><td></td></tr><tr><td>Control frame</td><td>Close</td><td>8</td><td>close(), onclose</td><td>The WebSocket closing handshake starts upon either sending or receiving a Close frame. It may prevent data loss by complementing the TCP closing handshake. No frame can be sent after sending a Close frame. If a Close frame is received and no prior Close frame was sent, a Close frame must be sent in response (typically echoing the status code received). The payload is optional, but if present, it must start with a two-byte big-endian unsigned integer status code, optionally followed by a UTF-8-encoded reason message not longer than 123 bytes.</td><td>Protocol state</td><td>No</td><td>125</td></tr><tr><td>Ping</td><td>9</td><td></td><td>May be used for latency measurement, keepalive and heartbeat. Both sides can send a ping (with any payload). Whoever receives it must, as soon as is practical, send back a pong with the same payload. A pong should be ignored if no prior ping was sent.</td></tr><tr><td>Pong</td><td>10</td><td></td></tr><tr><td></td><td>11–15</td><td></td><td>Reserved for further control frames. May be defined by an extension.</td></tr></tbody></table>
Client-to-server masking
A client must mask all frames sent to the server. A server must not mask any frames sent to the client. Frame masking applies XOR between the payload and the masking key. The following pseudocode describes the algorithm used to both mask and unmask a frame.
Status codes
<table><thead><tr><th>Range</th><th>Allowed in Close frame</th><th>Code</th><th>Description</th></tr></thead><tbody><tr><td>0–999</td><td>No</td><td></td><td>Unused</td></tr><tr><td>1000–2999 (Protocol)</td><td>Yes</td><td>1000</td><td>Normal closure.</td></tr><tr><td>1001</td><td>Going away (e.g. browser tab closed; server going down).</td></tr><tr><td>1002</td><td>Protocol error.</td></tr><tr><td>1003</td><td>Unsupported data (e.g. endpoint only understands text but received binary).</td></tr><tr><td>No</td><td>1004</td><td>Reserved for future usage</td></tr><tr><td>1005</td><td>No code received.</td></tr><tr><td>1006</td><td>Connection closed abnormally (i.e. closing handshake did not occur).</td></tr><tr><td>Yes</td><td>1007</td><td>Invalid payload data (e.g. non UTF-8 data in a text message).</td></tr><tr><td>1008</td><td>Policy violated.</td></tr><tr><td>1009</td><td>Message too big.</td></tr><tr><td>1010</td><td>Unsupported extension. The client should write the extensions it expected the server to support in the payload.</td></tr><tr><td>1011</td><td>Internal server error.</td></tr><tr><td>No</td><td>1015</td><td>TLS handshake failure.</td></tr><tr><td>3000–3999</td><td>Yes</td><td></td><td>Reserved for libraries, frameworks and applications. Registered directly with IANA.</td></tr><tr><td>4000–4999</td><td></td><td>Private use.</td></tr></tbody></table>
Compression extension
The permessage-deflate extension allows data messages to be compressed using the DEFLATE algorithm. For example, during the opening handshake, the client and server may use the following header to enable the extension. The RSV1 field of the first frame of a data message must be set to indicate the payload data is compressed.
Server implementation example
Note: recv() returns up to the amount of bytes requested. For readability, the code ignores that, thus it may fail in non-ideal network conditions.
Browser support
WebSocket
A secure version of the WebSocket protocol is implemented in Firefox 6, Safari 6, Google Chrome 14, Opera 12.10 and Internet Explorer 10. A detailed protocol test suite report lists the conformance of those browsers to specific protocol aspects.
An older, less secure version of the protocol was implemented in Opera 11 and Safari 5, as well as the mobile version of Safari in iOS 4.2. The BlackBerry Browser in OS7 implements WebSockets. Because of vulnerabilities, it was disabled in Firefox 4 and 5, and Opera 11. Using browser developer tools, developers can inspect the WebSocket handshake as well as the WebSocket frames.
<table><thead><tr><th>Protocol version</th><th>Draft date</th><th>Internet Explorer</th><th>Firefox (PC)</th><th>Firefox (Android)</th><th>Chrome (PC, Mobile)</th><th>Safari (Mac, iOS)</th><th>Opera (PC, Mobile)</th><th>Android Browser</th></tr></thead><tbody><tr><td>hixie-75</td><td>February 4, 2010</td><td></td><td></td><td></td><td>4</td><td>5.0.0</td><td></td><td></td></tr><tr><td>hixie-76 hybi-00</td><td>May 6, 2010 May 23, 2010</td><td></td><td>4.0 (disabled)</td><td></td><td>6</td><td>5.0.1</td><td>11.00 (disabled)</td><td></td></tr><tr><td>hybi-07, v7</td><td>April 22, 2011</td><td></td><td>6</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>hybi-10, v8</td><td>July 11, 2011</td><td></td><td>7</td><td>7</td><td>14</td><td></td><td></td><td></td></tr><tr><td>RFC 6455, v13</td><td>December, 2011</td><td>10</td><td>11</td><td>11</td><td>16</td><td>6</td><td>12.10</td><td>4.4</td></tr></tbody></table>
Server implementations
WebSocket
• Nginx has supported WebSockets since 2013, implemented in version 1.3.13 including acting as a reverse proxy and load balancer of WebSocket applications.
• Apache HTTP Server has supported WebSockets since July, 2013, implemented in version 2.4.5 • Internet Information Services added support for WebSockets in version 8 which was released with Windows Server 2012. • lighttpd has supported WebSockets since 2017, implemented in lighttpd 1.4.46. lighttpd modproxy can act as a reverse proxy and load balancer of WebSocket applications. lighttpd modwstunnel can act as a WebSocket endpoint to transmit arbitrary data, including in JSON format, to a backend application. lighttpd supports WebSockets over HTTP/2 since 2022, implemented in lighttpd 1.4.65. • Eclipse Mosquitto This is an MQTT broker, but it supports the MQTT over WebSocket. So, it can be considered a type of WebSocket implementation.
ASP.NET Core have support for WebSockets using the app.UseWebSockets(); middleware.
Security considerations
WebSocket
Unlike regular cross-domain HTTP requests, WebSocket requests are not restricted by the same-origin policy. Therefore, WebSocket servers must validate the "Origin" header against the expected origins during connection establishment, to avoid cross-site WebSocket hijacking attacks (similar to cross-site request forgery), which might be possible when the connection is authenticated with cookies or HTTP authentication. It is better to use tokens or similar protection mechanisms to authenticate the WebSocket connection when sensitive (private) data is being transferred over the WebSocket. A live example of vulnerability was seen in 2020 in the form of Cable Haunt.
Proxy traversal
WebSocket
WebSocket protocol client implementations try to detect whether the user agent is configured to use a proxy when connecting to destination host and port, and if it is, uses HTTP CONNECT method to set up a persistent tunnel.
The WebSocket protocol is unaware of proxy servers and firewalls. Some proxy servers are transparent and work fine with WebSocket; others will prevent WebSocket from working correctly, causing the connection to fail. In some cases, additional proxy-server configuration may be required, and certain proxy servers may need to be upgraded to support WebSocket.
If unencrypted WebSocket traffic flows through an explicit or a transparent proxy server without WebSockets support, the connection will likely fail.
If an encrypted WebSocket connection is used, then the use of Transport Layer Security (TLS) in the WebSocket Secure connection ensures that an HTTP CONNECT command is issued when the browser is configured to use an explicit proxy server. This sets up a tunnel, which provides low-level end-to-end TCP communication through the HTTP proxy, between the WebSocket Secure client and the WebSocket server. In the case of transparent proxy servers, the browser is unaware of the proxy server, so no HTTP CONNECT is sent. However, since the wire traffic is encrypted, intermediate transparent proxy servers may simply allow the encrypted traffic through, so there is a much better chance that the WebSocket connection will succeed if WebSocket Secure is used. Using encryption is not free of resource cost, but often provides the highest success rate, since it would be travelling through a secure tunnel.
A mid-2010 draft (version hixie-76) broke compatibility with reverse proxies and gateways by including eight bytes of key data after the headers, but not advertising that data in a Content-Length: 8 header. This data was not forwarded by all intermediates, which could lead to protocol failure. More recent drafts (e.g., hybi-09) put the key data in a Sec-WebSocket-Key header, solving this problem.