Skip to content

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.

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:

server.ts
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;
});
Terminal window
deno run --allow-net server.ts

The 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).

Deno has the browser’s WebSocket class as a global, so the client is the same code you’d write for a web page:

client.ts
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);
});
Terminal window
deno run --allow-net=localhost:8080 client.ts

Output from the two processes:

# server
Listening on http://localhost:8080/
Client connected
Received message: Hello, Deno!
Client disconnected: 1000
# client
Connected
Received from server: Echo: Hello, Deno!
Disconnected: 1000
  • Binary data. event.data is a string for text frames. For binary frames it’s a Blob by default in browsers and Deno clients; set ws.binaryType = "arraybuffer" to get an ArrayBuffer instead.
  • Broadcasting. Each connection has its own socket. To send to every client (a chat room, say), keep the open sockets in a Set, add on open, remove on close, and loop over it to send.
  • Secure connections. Use wss:// in production. If Deno serves TLS directly, pass cert and key to Deno.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 error and close) if the client doesn’t answer with a pong in time. The idleTimeout option to Deno.upgradeWebSocket sets that time in seconds; it defaults to 30, and 0 turns it off.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)