denoland/deno · error · TypeError

Socket is not a TCP socket - only TCP connections can be…

Error message

Socket is not a TCP socket - only TCP connections can be upgraded to WebSocket

What it means

To turn the node socket into a WebSocket, Deno calls `takeStream()` on the socket's native handle — a method only TCP handles expose in Deno's node compatibility layer. TLS-wrapped sockets (`TLSSocket`), Unix-domain/named-pipe sockets, and IPC channels do not have it, so the upgrade is refused with this TypeError rather than corrupting the connection.

Solutions

  1. Serve WebSocket upgrades on a plain TCP listener (terminate TLS at a proxy in front if needed)
  2. For secure WS, use Deno's native `Deno.serve` over TLS with `Deno.upgradeWebSocket`, which does not go through the node socket path
  3. Check `typeof socket._handle?.takeStream === 'function'` before attempting the upgrade

Example fix

// before
const tcpServer = nodeNet.createServer();
tcpServer.listen({ path: "/tmp/app.sock" });
tcpServer.on("upgrade", (req, socket) => {
  Deno.upgradeWebSocket(req, { socket }); // UDS socket -> TypeError
});

// after
const tcpServer = nodeNet.createServer();
tcpServer.listen({ port: 8080 }); // plain TCP
const httpServer = nodeHttp.createServer();
httpServer.on("upgrade", (req, socket) => {
  Deno.upgradeWebSocket(req, { socket }); // TCP handle has takeStream
});
Defensive patterns

Strategy: validation

Validate before calling

const handle = (socket as any)._handle;
if (typeof handle?.takeStream !== "function") {
  socket.destroy(); // not a TCP socket; cannot become a WebSocket
  return;
}
const { response } = Deno.upgradeWebSocket(req, { socket });

Type guard

const isTcpNodeSocket = (
  s: { destroyed?: boolean; _handle?: { takeStream?: unknown } },
): boolean =>
  !s.destroyed && !!s._handle && typeof s._handle.takeStream === "function";

Prevention

When it happens

Trigger: Invoking the node upgrade path on a connection from a `tls.Server` (`secureConnection`/TLS-wrapped socket); serving on `server.listen({ path: '/tmp/app.sock' })` (pipe/UDS) and upgrading; upgrading a child_process IPC channel socket.

Common situations: HTTPS/WSS setups expecting the same upgrade flow as plain HTTP; microservices communicating over Unix domain sockets; polyglot setups piping Node servers.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/5f32e409841a89b4. Report an issue: GitHub.

Appendix: source

Thrown at ext/http/02_websocket.ts:165

    // sync with the header-list built above (protocol negotiation, etc.).
    let responseHead = "HTTP/1.1 101 Switching Protocols\r\n";
    for (let i = 0; i < r.headerList.length; i++) {
      const { 0: name, 1: value } = r.headerList[i];
      responseHead += `${name}: ${value}\r\n`;
    }
    responseHead += "\r\n";

    if (nodeSocket.destroyed) {
      throw new TypeError(
        "Socket is already destroyed - cannot upgrade to WebSocket",
      );
    }
    const handle = nodeSocket._handle;
    if (!handle) {
      throw new TypeError("Socket has no handle - cannot upgrade");
    }
    if (typeof handle.takeStream !== "function") {
      throw new TypeError(
        "Socket is not a TCP socket - only TCP connections can be upgraded to WebSocket",
      );
    }

    // Extra bytes that were already buffered (e.g., from the upgrade
    // request body that arrived with the headers)
    const extraBytes = options.head || new Uint8Array(0);

    // Defer setup so the caller can attach event handlers (onopen,
    // onmessage, etc.) before events fire.
    (async () => {
      try {
        // Wait for the 101 response to fully flush before taking the
        // stream. A fire-and-forget write could leave data in the
        // internal_write_queue that would be orphaned once we detach
        // the stream from libuv.
        await new Promise((resolve, reject) => {
          nodeSocket.write(responseHead, (err) => {

View on GitHub (pinned to 9ad36f7a2c)