← ClaudeAtlas

bun-websocketslisted

Server-side WebSockets in Bun
jarle/bun-skills · ★ 5 · Web & Frontend · score 63
Install: claude install-skill jarle/bun-skills
# WebSockets > Server-side WebSockets in Bun `Bun.serve()` supports server-side WebSockets, with on-the-fly compression, TLS support, and a Bun-native publish-subscribe API. <Info> **⚡️ 7x more throughput** Bun's WebSockets are fast. For a [simple chatroom](https://github.com/oven-sh/bun/tree/main/bench/websocket-server/README.md) on Linux x64, Bun can handle 7x more requests per second than Node.js + [`"ws"`](https://github.com/websockets/ws). | **Messages sent per second** | **Runtime** | **Clients** | | ---------------------------- | ------------------------------ | ----------- | | \~700,000 | (`Bun.serve`) Bun v0.2.1 (x64) | 16 | | \~100,000 | (`ws`) Node v18.10.0 (x64) | 16 | Internally Bun's WebSocket implementation is built on [uWebSockets](https://github.com/uNetworking/uWebSockets). </Info> *** ## Start a WebSocket server Below is a simple WebSocket server built with `Bun.serve`, in which all incoming requests are [upgraded](https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism) to WebSocket connections in the `fetch` handler. The socket handlers are declared in the `websocket` parameter. ```ts server.ts icon="https://mintcdn.com/bun-1dd33a4e/nIz6GtMH5K-dfXeV/icons/typescript.svg?fit=max&auto=format&n=nIz6GtMH5K-dfXeV&q=85&s=5d73d76daf7eb7b158469d8c30d349b0" theme={"theme":{"light":"github-light","dark":"dracula"}} Bun.serve({ fetch(req, ser