Skip to content

acceptWebSocket

acceptWebSocket(request, options, onOpen): Promise<HttpOutgoingResponse>

Defined in: packages/net-websocket/src/handshake/accept-websocket.ts:64

Answers a request to open a WebSocket: validates the opening handshake and returns the response a handler should return.

Accepts both forms: an HTTP/1.1 GET with Upgrade: websocket (RFC 6455 §4.2), answered 101 with its Sec-WebSocket-Accept, and an extended CONNECT with protocol websocket (RFC 8441 for HTTP/2, RFC 9220 for HTTP/3), answered 200. Once the response is sent the connection or stream becomes a WebSocketPeer, handed to onOpen.

An invalid handshake is answered 400, and an unsupported Sec-WebSocket-Version 426 listing version 13; onOpen is then never called. Checking Origin — what stops another site’s page from opening a WebSocket with the user’s cookies — is the caller’s job, before calling this.

HttpIncomingRequest

WebSocketAcceptOptions

(peer) => void

Promise<HttpOutgoingResponse>

const handler: HttpHandler = (request) =>
acceptWebSocket(
request,
{ maxMessageSize: 1 << 20, closeTimeoutMs: 5000 },
(peer) => {
peer.subscribe("message", (data) => void peer.send(data));
},
);
```;