WebSockets in Deno: an echo server and client

WebSockets give a client and server a long-lived, two-way connection: after an HTTP upgrade handshake, either side can send messages at any time without a new request. They suit chat, live notifications, multiplayer games and streaming data feeds. Deno supports both ends with no imports.
The server
Section titled “The server”A WebSocket connection starts as an HTTP request with an Upgrade: websocket header. Deno.serve handles the HTTP side, and Deno.upgradeWebSocket turns that request into a socket plus the 101 Switching Protocols response you return:
Deno.serve({ port: 8080 }, (req) => { if (req.headers.get("upgrade") !== "websocket") { return new Response("Expected a WebSocket upgrade", { status: 426 }); }
const { socket, response } = Deno.upgradeWebSocket(req);
socket.addEventListener("open", () => { console.log("Client connected"); });
socket.addEventListener("message", (event) => { console.log("Received message:", event.data); socket.send(`Echo: ${event.data}`); });
socket.addEventListener("close", (event) => { console.log("Client disconnected:", event.code); });
socket.addEventListener("error", (event) => { console.error("WebSocket error:", event); });
return response;});deno run --allow-net server.tsThe socket is the same WebSocket type a browser uses, so you work with it through events and send() rather than a for await loop. Don’t call socket.send() until the open event has fired; the handshake isn’t finished when the handler returns.
Plain HTTP requests to the same port get a 426 Upgrade Required, which makes it easy to check the server is up with curl -i localhost:8080. In a real app you’d route on the path instead (for example, upgrade only on /ws and serve pages everywhere else).
The client
Section titled “The client”Deno has the browser’s WebSocket class as a global, so the client is the same code you’d write for a web page:
const ws = new WebSocket("ws://localhost:8080");
ws.addEventListener("open", () => { console.log("Connected"); ws.send("Hello, Deno!");});
ws.addEventListener("message", (event) => { console.log("Received from server:", event.data); ws.close(1000, "done");});
ws.addEventListener("close", (event) => { console.log("Disconnected:", event.code);});deno run --allow-net=localhost:8080 client.tsOutput from the two processes:
# serverListening on http://localhost:8080/Client connectedReceived message: Hello, Deno!Client disconnected: 1000
# clientConnectedReceived from server: Echo: Hello, Deno!Disconnected: 1000Things to watch for
Section titled “Things to watch for”- Binary data.
event.datais a string for text frames. For binary frames it’s aBlobby default in browsers and Deno clients; setws.binaryType = "arraybuffer"to get anArrayBufferinstead. - Broadcasting. Each connection has its own
socket. To send to every client (a chat room, say), keep the open sockets in aSet, add onopen, remove onclose, and loop over it to send. - Secure connections. Use
wss://in production. If Deno serves TLS directly, passcertandkeytoDeno.serve; more often a reverse proxy or your hosting platform terminates TLS and forwards the upgrade. - Dead connections. Deno pings clients and closes a socket (firing
errorandclose) if the client doesn’t answer with a pong in time. TheidleTimeoutoption toDeno.upgradeWebSocketsets that time in seconds; it defaults to 30, and0turns it off.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)