Note: This feature is available in Web Workers.
The WebSocket API makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive responses without having to poll the server for a reply.
The WebSocket API provides two alternative mechanisms for creating and using web socket connections: the WebSocket interface and the WebSocketStream interface.
WebSocket interface is stable and has good browser and server support. However it doesn't support backpressure. As a result, when messages arrive faster than the application can process them it will either fill up the device's memory by buffering those messages, become unresponsive due to 100% CPU usage, or both.WebSocketStream interface is a Promise-based alternative to WebSocket. It uses the Streams API to handle receiving and sending messages, meaning that socket connections can take advantage of stream backpressure automatically, regulating the speed of reading or writing to avoid bottlenecks in the application. However, WebSocketStream is non-standard and currently only supported in one rendering engine.Additionally, the WebTransport API is expected to replace the WebSocket API for many applications. WebTransport is a versatile, low-level API that provides backpressure and many other features not supported by either WebSocket or WebSocketStream, such as unidirectional streams, out-of-order delivery, and unreliable data transmission via datagrams. WebTransport is more complex to use than WebSockets and its cross-browser support is not as wide, but it enables the implementation of sophisticated solutions. If standard WebSocket connections are a good fit for your use case and you need wide browser compatibility, you should employ the WebSockets API to get up and running quickly. However, if your application requires a non-standard custom solution, then you should use the WebTransport API.
Note: If a page has an open WebSocket connection, the browser may not add it to the bfcache. It's therefore good practice to close the connection when the user has finished with the page. See working with the bfcache.
WebSocketThe primary interface for connecting to a WebSocket server and then sending and receiving data on the connection.
WebSocketStream Non-standard
Promise-based interface for connecting to a WebSocket server; uses streams to send and receive data on the connection.
CloseEventThe event sent by the WebSocket object when the connection closes.
MessageEventThe event sent by the WebSocket object when a message is received from the server.
The HTTP headers are used in the WebSocket handshake:
Sec-WebSocket-KeyAn HTTP request header that contains a nonce from the client. This is used in the WebSocket opening handshake to verify that the client explicitly intends to open a WebSocket. It is added automatically by the browser.
Sec-WebSocket-AcceptAn HTTP response header used in the WebSocket opening handshake to indicate that the server is willing to upgrade to a WebSocket connection. The value in the directive is calculated from the value of Sec-WebSocket-Key in the corresponding request.
Sec-WebSocket-VersionAn HTTP header that in requests indicates the version of the WebSocket protocol understood by the client. In responses, it is sent only if the requested protocol version is not supported by the server, and lists the versions that the server supports.
Sec-WebSocket-ProtocolAn HTTP header that in requests indicates the sub-protocols supported by the client in preferred order. In responses, it indicates the sub-protocol selected by the server from the client's preferences.
Sec-WebSocket-ExtensionsAn HTTP header that in requests indicates the WebSocket extensions supported by the client in preferred order. In responses, it indicates the extension selected by the server from the client's preferences.
wss:// or ws:// and normal sockets over ssl://, tcp://
| Specification |
|---|
| WebSockets> # the-websocket-interface> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
WebSocket |
5 | 12 | 117–11 | 12.1 | 5 | 18 | 147–14 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
WebSockets_API |
5 | 12 | 117–11Message size limited to 16 MB (see bug 711205). |
12.1 | 5 | 18 | 147–14Message size limited to 16 MB (see bug 711205). |
12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
binaryType |
15 | 12 | 11 | 12.1 | 6 | 18 | 14 | 12.1 | 6 | 1.0 | 4.4 | 6 |
bufferedAmount |
5 | 12 | 7 | 12.1 | 5 | 18 | 7 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
close |
5 | 12 | 7 | 12.1 | 5 | 18 | 7 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
close_event |
5 | 12 | 7 | 12.1 | 5 | 18 | 7 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
error_event |
5 | 12 | 7 | 12.1 | 5 | 18 | 7 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
extensions |
16 | 12 | 8 | 12.1 | 6 | 18 | 8 | 12.1 | 6 | 1.0 | 4.4 | 6 |
message_event |
5 | 12 | 7 | 12.1 | 5 | 18 | 7 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
open_event |
5 | 12 | 7 | 12.1 | 5 | 18 | 7 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
protocol |
15 | 12 | 7 | 12.1 | 6 | 18 | 7 | 12.1 | 6 | 1.0 | 4.4 | 6 |
protocol_rfc_6455 |
16 | 12 | 11 | 15 | 6 | 18 | 14 | 14 | 6 | 1.0 | 4.4 | 6 |
readyState |
5 | 12 | 7 | 12.1 | 5 | 18 | 7 | 12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
send |
5 | 12 | 1811–18Only parameter of typeArrayBuffer and String supported.8–11Only parameter of typeString supported.7–8Only parameter of typeString supported. Returns boolean. |
12.1 | 5 | 18 | 1814–18Only parameter of typeArrayBuffer and String supported.8–14Only parameter of typeString supported.7–8Only parameter of typeString supported. Returns boolean. |
12.1 | 4.2 | 1.0 | 4.4 | 4.2 |
url |
18 | 12 | 7 | 12.1 | 6 | 18 | 7 | 12.1 | 6 | 1.0 | 4.4 | 6 |
worker_support |
5 | 12 | 37 | 12.1 | 5 | 18 | 37 | 12.1 | 5 | 1.0 | 4.4 | 5 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
WebSocketStream |
124 | 124 | No | 110 | No | 124 | No | 82 | No | 27.0 | 124 | No |
WebSockets_API |
124 | 124 | No | 110 | No | 124 | No | 82 | No | 27.0 | 124 | No |
close |
124 | 124 | No | 110 | No | 124 | No | 82 | No | 27.0 | 124 | No |
closed |
124 | 124 | No | 110 | No | 124 | No | 82 | No | 27.0 | 124 | No |
opened |
124 | 124 | No | 110 | No | 124 | No | 82 | No | 27.0 | 124 | No |
url |
124 | 124 | No | 110 | No | 124 | No | 82 | No | 27.0 | 124 | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API