Skip to main content

property Deno.UpgradeWebSocketOptions.socket

A node:net Socket from a node:http server's "upgrade" event. When provided, the WebSocket upgrade is performed over this existing TCP connection instead of through Deno.serve's built-in upgrade mechanism. The 101 Switching Protocols response is written automatically.

import http from "node:http";

const server = http.createServer();
server.on("upgrade", (req, socket, head) => {
  const { socket: ws } = Deno.upgradeWebSocket(
    new Request(`http://${req.headers.host}/`, {
      headers: req.headers as HeadersInit,
    }),
    { socket: socket as import("node:net").Socket, head },
  );
  ws.onmessage = (e) => ws.send(e.data);
});

Type #

import("node:net").Socket

Did you find what you needed?

Privacy policy