denoland/deno · error · TypeError
Protocol ' ' not in the request's protocol list (non…
Error message
Protocol '${options.protocol}' not in the request's protocol list (non negotiable) What it means
`Deno.upgradeWebSocket(request, { protocol })` is non-negotiable by design: if you request a subprotocol, it must appear verbatim in the request's `Sec-WebSocket-Protocol` list (split on ', '); otherwise this TypeError is thrown. The server may not answer with a protocol the client never offered — the browser would fail the connection on a mismatched `Sec-WebSocket-Protocol` response.
Solutions
- Have the client send it: `new WebSocket(url, 'chat')`
- Omit `protocol` in the options when you do not strictly require one
- Echo a protocol chosen from `request.headers.get('sec-websocket-protocol')` instead of a fixed value
Example fix
// before (server)
Deno.upgradeWebSocket(request, { protocol: "v2" });
// client: new WebSocket(url) // -> TypeError on server
// after
// client: new WebSocket(url, "v2")
Deno.upgradeWebSocket(request, { protocol: "v2" }); Defensive patterns
Strategy: validation
Validate before calling
const offered = (request.headers.get("sec-websocket-protocol") ?? "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const protocol = offered[0]; // choose from what the client offered
const ws = Deno.upgradeWebSocket(
request,
protocol ? { protocol } : {},
); Try / catch
try {
return Deno.upgradeWebSocket(request, { protocol: "chat" });
} catch (err) {
if (err instanceof TypeError && err.message.includes("protocol list")) {
return Deno.upgradeWebSocket(request); // retry without a fixed protocol
}
throw err;
} Prevention
- Make the client pass the subprotocol: new WebSocket(url, "chat")
- Derive the chosen protocol from the request's Sec-WebSocket-Protocol list instead of a constant
- Keep subprotocol names in one shared constant on client and server
When it happens
Trigger: Server passes `protocol: 'chat'` but the client connected with `new WebSocket(url)` (no subprotocols) or offered only different ones (e.g. `['graphql']`).
Common situations: Hardcoding a subprotocol server-side while clients forget to pass it; version skew between client and server protocol names; case or whitespace mismatches in protocol names.
Related errors
- Already closed
- Already upgraded
- ERR_HTTP2_STATUS_101
- h2 stream closed
- Invalid Header: 'connection' header must contain 'Upgrade'
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/2b1e2e62913a24b0.
Report an issue: GitHub.
Appendix: source
Thrown at ext/http/02_websocket.ts:86
const accept = op_http_websocket_accept_header(websocketKey);
const r = newInnerResponse(101);
r.headerList = [
["upgrade", "websocket"],
["connection", "Upgrade"],
["sec-websocket-accept", accept],
];
const protocolsStr = request.headers.get("sec-websocket-protocol") || "";
const protocols = StringPrototypeSplit(protocolsStr, ", ");
if (protocols && options.protocol) {
if (ArrayPrototypeIncludes(protocols, options.protocol)) {
ArrayPrototypePush(r.headerList, [
"sec-websocket-protocol",
options.protocol,
]);
} else {
throw new TypeError(
`Protocol '${options.protocol}' not in the request's protocol list (non negotiable)`,
);
}
}
const {
_eventLoop,
_idleTimeoutDuration,
_idleTimeoutTimeout,
_readyState,
_rid,
_role,
_serverHandleIdleTimeout,
createWebSocketBranded,
installServerInspector,
SERVER,
WebSocket,
} = loadWebSocket();View on GitHub (pinned to 9ad36f7a2c)