Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Note: This feature is available in Web Workers.
The WebSocketStream interface of the WebSockets API is a promise-based API for connecting to a WebSocket server. It uses streams to send and receive data on the connection, and can therefore take advantage of stream backpressure automatically, regulating the speed of reading or writing to avoid bottlenecks in the application.
WebSocketStream() Experimental
Creates a new WebSocketStream object instance.
url Read only Experimental
Returns the URL of the WebSocket server that the WebSocketStream instance was created with.
closed Read only Experimental
Returns a Promise that fulfills with an object once the socket connection is closed. The object contains the closing code and reason as sent by the server.
opened Read only Experimental
Returns a Promise that fulfills with an object once the socket connection is successfully opened. Among other features, this object contains a ReadableStream and a WritableStream instance for receiving and sending data on the connection.
close() Experimental
Closes the WebSocket connection.
const output = document.querySelector("#output");
function writeToScreen(message) {
const pElem = document.createElement("p");
pElem.textContent = message;
output.appendChild(pElem);
}
if (!("WebSocketStream" in self)) {
writeToScreen("Your browser does not support WebSocketStream");
} else {
const wsURL = "ws://127.0.0.1/";
const wss = new WebSocketStream(wsURL);
console.log(wss.url);
async function start() {
const { readable, writable, extensions, protocol } = await wss.opened;
writeToScreen("CONNECTED");
closeBtn.disabled = false;
const reader = readable.getReader();
const writer = writable.getWriter();
writer.write("ping");
writeToScreen("SENT: ping");
while (true) {
const { value, done } = await reader.read();
writeToScreen(`RECEIVED: ${value}`);
if (done) {
break;
}
setTimeout(() => {
writer.write("ping");
writeToScreen("SENT: ping");
}, 5000);
}
}
start();
}
See Using WebSocketStream to write a client for a complete example with full explanation.
Not currently a part of any specification. See https://github.com/whatwg/websockets/pull/48 for standardization progress.
| 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 |
WebSocketStream |
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/WebSocketStream