secondsky avatar

Bun Websocket Server

A solid reference for Bun's native WebSocket implementation that covers the essentials without the usual Node.js ceremony. You get the upgrade pattern with Bun.serve, built-in pub/sub with topics for broadcasting, and typed data attachment per connection. The examples are practical, covering chat rooms, authentication gates, and server-side publishing outside request handlers. Honestly, the pub/sub being native is the real win here. You don't need a separate library for room-based messaging. Good for real-time features where you want fewer dependencies and tighter integration with Bun's HTTP layer.

skills.sh installs
326
GitHub stars
248
last updated
Aug 21, 2026

Install

$ npx -y skills add secondsky/claude-skills --skill bun-websocket-server --agent claude-code

Installs into .claude/skills of the current project.

Files

Bun WebSocket Server

Bun has built-in WebSocket support integrated with Bun.serve().

Quick Start

const server = Bun.serve({
  fetch(req, server) {
    // Upgrade to WebSocket
    if (server.upgrade(req)) {
      return; // Upgraded successfully
    }
    return new Response("Not a WebSocket request", { status: 400 });
  },
  websocket: {
    open(ws) {
      console.log("Client connected");
    },
    message(ws, message) {
      console.log("Received:", message);
      ws.send(`Echo: ${message}`);
    },
    close(ws) {
      console.log("Client disconnected");
    },
  },
});

console.log(`WebSocket server running on ws://localhost:${server.port}`);

WebSocket Handlers

Bun.serve({
  fetch(req, server) {
    server.upgrade(req);
  },
  websocket: {
    // Client connected
    open(ws) {
      console.log("New connection");
    },

    // Message received
    message(ws, message) {
      // message is string | Buffer
      if (typeof message === "string") {
        console.log("Text:", message);
      } else {
        console.log("Binary:", message);
      }
    },

Sponsors

Details

Repository
github.com/secondsky/claude-skills
Category
Backend & APIs
First seen
Jun 3, 2026

More from secondsky/claude-skills

All 146
1TryGhost avatarAdd Admin API EndpointTryGhost/GhostAdd a new endpoint or endpoints to Ghost's Admin API at `ghost/api/admin/**`.0skills.sh installs
2giuseppe-trisciuoglio avatarspring-boot-rest-api-standardsgiuseppe-trisciuoglio/developer-kitProvides REST API design standards and best practices for Spring Boot projects. Use when creating or reviewing REST endpoints, DTOs, error handling, pagination,4.3Kskills.sh installs
3giuseppe-trisciuoglio avatarunit-test-wiremock-rest-apigiuseppe-trisciuoglio/developer-kitProvides patterns for unit testing external REST APIs using WireMock. Stubs API responses, verifies request details, simulates failures (timeouts, 4xx/5xx error3.9Kskills.sh installs
4aj-geddes avatarrest-api-designaj-geddes/useful-ai-promptsDesign RESTful APIs following best practices for resource modeling, HTTP methods, status codes, versioning, and documentation. Use when creating new APIs, desig2.1Kskills.sh installs
5manutej avatarpostgresql-database-engineeringmanutej/luxor-claude-marketplaceComprehensive PostgreSQL database engineering skill covering indexing strategies, query optimization, performance tuning, partitioning, replication, backup and 1.8Kskills.sh installs
6ruvnet avataragent-dev-backend-apiruvnet/rufloAgent skill for dev-backend-api - invoke with $agent-dev-backend-api1.1Kskills.sh installs