files/en-us/web/api/websocketstream/closed/index.md
{{APIRef("WebSockets API")}}{{SeeCompatTable}}{{AvailableInWorkers}}
The closed read-only property of the
{{domxref("WebSocketStream")}} interface returns a {{jsxref("Promise")}} that fulfills with an object once the socket connection is closed. The object contains the closing code and reason.
A promise, which fulfills with an object containing the following properties:
closeCode
CloseEvent status codes).reason
The promise rejects if the WebSocket connection did not close cleanly (for a clean close, the associated TCP connection must be closed after the WebSocket closing handshake is completed).
const wsURL = "wss://127.0.0.1/";
const wss = new WebSocketStream(wsURL);
wss.closed.then((result) => {
writeToScreen(
`DISCONNECTED: code ${result.closeCode}, message "${result.reason}"`,
);
console.log("Socket closed", result.closeCode, result.reason);
});
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}}