This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Note: This feature is available in Web Workers.
The WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.
To construct a WebSocket, use the WebSocket() constructor.
Note: The WebSocket API has no way to apply backpressure, therefore when messages arrive faster than the application can process them, the application will either fill up the device's memory by buffering those messages, become unresponsive due to 100% CPU usage, or both. For an alternative that provides backpressure automatically, see WebSocketStream.
WebSocket()Returns a newly created WebSocket object.
WebSocket.binaryTypeThe binary data type used by the connection.
WebSocket.bufferedAmount Read only
The number of bytes of queued data.
WebSocket.extensions Read only
The extensions selected by the server.
WebSocket.protocol Read only
The sub-protocol selected by the server.
WebSocket.readyState Read only
The current state of the connection.
WebSocket.url Read only
The absolute URL of the WebSocket.
WebSocket.close()Closes the connection.
WebSocket.send()Enqueues data to be transmitted.
Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.
closeFired when a connection with a WebSocket is closed. Also available via the onclose property
errorFired when a connection with a WebSocket has been closed because of an error, such as when some data couldn't be sent. Also available via the onerror property.
messageFired when data is received through a WebSocket. Also available via the onmessage property.
openFired when a connection with a WebSocket is opened. Also available via the onopen property.
// Create WebSocket connection.
const socket = new WebSocket("ws://localhost:8080");
// Connection opened
socket.addEventListener("open", (event) => {
socket.send("Hello Server!");
});
// Listen for messages
socket.addEventListener("message", (event) => {
console.log("Message from server ", event.data);
});
| 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 |
WebSocket |
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 |
© 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/WebSocket