Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions files/en-us/web/api/websocketstream/websocketstream/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ new WebSocketStream(url, options)
- `protocols` {{optional_inline}}
- : A single string or an array of strings representing the sub-protocol(s) that the client would like to use, for example `"amqp"` or `"mqtt"`. Subprotocols may be selected from the [IANA WebSocket Subprotocol Name Registry](https://www.iana.org/assignments/websocket/websocket.xml#subprotocol-name) or may be custom names jointly understood by the client and the server. A single server can implement multiple WebSocket sub-protocols, and handle different types of interactions depending on the specified value. If it is omitted, an empty array is used by default. If `protocols` is included, the connection will only be established if the server reports that it has selected one of these sub-protocols.
- `signal` {{optional_inline}}
- : An {{domxref("AbortSignal")}} belonging to an {{domxref("AbortController")}} that you want to use to close the WebSocket connection.
- : An {{domxref("AbortSignal")}} belonging to an {{domxref("AbortController")}}, which can be used to abort the *handshake*. This is specifically intended to make it easy to implement connection timeouts. As such, it does nothing after the connection is established.


### Exceptions

Expand All @@ -47,26 +48,28 @@ const wss = new WebSocketStream("wss://example.com/wss");
A more advanced example could also include an options object containing custom protocols and/or an {{domxref("AbortSignal")}}:

```js
const controller = new AbortController();
const queueWSS = new WebSocketStream("wss://example.com/queue", {
protocols: ["amqp", "mqtt"],
signal: controller.signal,
signal: AbortSignal.timeout(5000),
});
```

At a later time, {{domxref("AbortController.abort()")}} can be called when required to close the connection:
and the connect will timeout after 5 seconds.

```js
controller.abort();
```
If you're connecting to localhost, it's likely to succeed or fail almost instantly, so this will have no effect.

Alternatively, you can use the {{domxref("WebSocketStream.close()")}} method to close a connection, however this is mainly needed if you wish to specify a custom code and/or reason for the server to report.
You can use the {{domxref("WebSocketStream.close()")}} method to close a connection.

`WritableStream.close()` and `WritableStreamDefaultWriter.close()` from the `writable` of the fulfilled `opened` `Promise` also closes the connection.

Copy link
Collaborator

@wbamberg wbamberg Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd lean towards omitting this, as it looks a bit random after this update.

Suggested change
You can use the {{domxref("WebSocketStream.close()")}} method to close a connection.
`WritableStream.close()` and `WritableStreamDefaultWriter.close()` from the `writable` of the fulfilled `opened` `Promise` also closes the connection.

I suppose the argument for keeping it is to forestall confusion about using signal to close the connection. If we wanted to do that, we ought to frame it properly: "Note that you can't use signal to close a connection once it has been established. To close an established connection...". And we ought to put that explanation in the main body of the page (perhaps in the signal <dd>) and not in the examples (examples should illustrate, not explain new things).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to keep it, and to add something along the lines of what you wrote. Can you help author that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's going to be much easier to do that as a follow up then, so I'll merge this as it is.

See [Using WebSocketStream to write a client](/en-US/docs/Web/API/WebSockets_API/Using_WebSocketStream) for a complete example with full explanation.

## Specifications

Not currently a part of any specification. See https://github.com/whatwg/websockets/pull/48 for standardization progress.
Not currently a part of any specification. See https://github.com/whatwg/websockets/pull/48 for standardization progress.

[WebSocketStream API design](https://docs.google.com/document/d/1La1ehXw76HP6n1uUeks-WJGFgAnpX2tCjKts7QFJ57Y/edit?pli=1&tab=t.0).


## Browser compatibility

Expand Down