Skip to main content
Bun’s server-side WebSocket API provides a native pub-sub API. Sockets can be subscribed to a set of named channels using socket.subscribe(<name>); messages can be published to a channel using socket.publish(<name>, <message>). This code snippet implements a simple single-channel chat server.
https://mintcdn.com/bun-1dd33a4e-claude-fix-yaml-docs/ba6flgIPltjjNPcD/icons/typescript.svg?fit=max&auto=format&n=ba6flgIPltjjNPcD&q=85&s=9c700644ac7789bb180093029561ecd4server.ts
const server = Bun.serve<{ username: string }>({
  fetch(req, server) {
    const cookies = req.headers.get("cookie");
    const username = getUsernameFromCookies(cookies);
    const success = server.upgrade(req, { data: { username } });
    if (success) return undefined;

    return new Response("Hello world");
  },
  websocket: {
    open(ws) {
      const msg = `${ws.data.username} has entered the chat`;
      ws.subscribe("the-group-chat");
      server.publish("the-group-chat", msg);
    },
    message(ws, message) {
      // the server re-broadcasts incoming messages to everyone
      server.publish("the-group-chat", `${ws.data.username}: ${message}`);
    },
    close(ws) {
      const msg = `${ws.data.username} has left the chat`;
      server.publish("the-group-chat", msg);
      ws.unsubscribe("the-group-chat");
    },
  },
});

console.log(`Listening on ${server.hostname}:${server.port}`);