Get to know MDN better
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.
Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.
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.
Creates a new WebSocketStream object instance.
Returns the URL of the WebSocket server that the WebSocketStream instance was created with.
closed Read onlyReturns 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 onlyReturns 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.
Closes the WebSocket connection.
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.
Enable JavaScript to view this browser compatibility table.
This page was last modified on May 9, 2026 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.