files/en-us/web/api/websocketstream/opened/index.md
{{APIRef("WebSockets API")}}{{SeeCompatTable}}{{AvailableInWorkers}}
The opened read-only property of the
{{domxref("WebSocketStream")}} interface returns a {{jsxref("Promise")}} that fulfills with an object once the socket connection is successfully opened. Among other features, this object contains a {{domxref("ReadableStream")}} and a {{domxref("WritableStream")}} instance for receiving and sending data on the connection.
A promise, which fulfills with an object containing the following properties:
extensions
WebSocketStream. Such extensions are not currently defined, but may be in the future. Currently returns an empty string.protocol
protocols option of the WebSocketStream() constructor). Returns an empty string if no sub-protocol has been used to open the connection (i.e., no sub-protocol options were included in the constructor call).readable
writable
The promise rejects if the WebSocket connection fails.
const wsURL = "wss://127.0.0.1/";
const wss = new WebSocketStream(wsURL);
async function start() {
const { readable, writable, extensions, protocol } = await wss.opened;
const reader = readable.getReader();
const writer = writable.getWriter();
writer.write("ping");
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
setTimeout(() => {
writer.write("ping");
}, 5000);
}
}
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.
{{Compat}}