{"record":{"id":"2b1e2e62913a24b0","repo":"denoland/deno","slug":"protocol-options-protocol-not-in-the-request","errorCode":null,"errorMessage":"Protocol '${options.protocol}' not in the request's protocol list (non negotiable)","messagePattern":"Protocol '(.+?)' not in the request's protocol list \\(non negotiable\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/http/02_websocket.ts","lineNumber":86,"sourceCode":"  const accept = op_http_websocket_accept_header(websocketKey);\n\n  const r = newInnerResponse(101);\n  r.headerList = [\n    [\"upgrade\", \"websocket\"],\n    [\"connection\", \"Upgrade\"],\n    [\"sec-websocket-accept\", accept],\n  ];\n\n  const protocolsStr = request.headers.get(\"sec-websocket-protocol\") || \"\";\n  const protocols = StringPrototypeSplit(protocolsStr, \", \");\n  if (protocols && options.protocol) {\n    if (ArrayPrototypeIncludes(protocols, options.protocol)) {\n      ArrayPrototypePush(r.headerList, [\n        \"sec-websocket-protocol\",\n        options.protocol,\n      ]);\n    } else {\n      throw new TypeError(\n        `Protocol '${options.protocol}' not in the request's protocol list (non negotiable)`,\n      );\n    }\n  }\n\n  const {\n    _eventLoop,\n    _idleTimeoutDuration,\n    _idleTimeoutTimeout,\n    _readyState,\n    _rid,\n    _role,\n    _serverHandleIdleTimeout,\n    createWebSocketBranded,\n    installServerInspector,\n    SERVER,\n    WebSocket,\n  } = loadWebSocket();","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/http/02_websocket.ts#L68-L104","documentation":"`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.","triggerScenarios":"Server passes `protocol: 'chat'` but the client connected with `new WebSocket(url)` (no subprotocols) or offered only different ones (e.g. `['graphql']`).","commonSituations":"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.","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"],"exampleFix":"// before (server)\nDeno.upgradeWebSocket(request, { protocol: \"v2\" });\n// client: new WebSocket(url)            // -> TypeError on server\n\n// after\n// client: new WebSocket(url, \"v2\")\nDeno.upgradeWebSocket(request, { protocol: \"v2\" });","handlingStrategy":"validation","validationCode":"const offered = (request.headers.get(\"sec-websocket-protocol\") ?? \"\")\n  .split(\",\")\n  .map((s) => s.trim())\n  .filter(Boolean);\nconst protocol = offered[0]; // choose from what the client offered\nconst ws = Deno.upgradeWebSocket(\n  request,\n  protocol ? { protocol } : {},\n);","typeGuard":null,"tryCatchPattern":"try {\n  return Deno.upgradeWebSocket(request, { protocol: \"chat\" });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes(\"protocol list\")) {\n    return Deno.upgradeWebSocket(request); // retry without a fixed protocol\n  }\n  throw err;\n}","preventionTips":["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"],"tags":["websocket","subprotocol","negotiation"],"backgroundTag":"websocket-protocol-mismatch","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}