Registry / messaging / bun-ws-router

bun-ws-router

JSON →
library0.6.1jsnpmunverified

Lightweight WebSocket router for the Bun runtime, version 0.6.1. Released with monthly cadence, supports both client and server-side WebSocket routing with Zod/Valibot schema validation for type-safe messaging. Key differentiators: built specifically for Bun (uses Bun's WebSocket API), supports broadcast, pub/sub, and middleware patterns, and integrates with Hono. Requires zod >=4.0.14 or valibot >=1.1.0. Ideal for real-time applications on Bun.

npm install bun-ws-router
INSTALL
IMPORT
SIG · BUN-WS-ROUTER
B
bun-ws-router
messagingjavascriptv0.6.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

defineRouter
import { defineRouter } from 'bun-ws-router'
const { defineRouter } = require('bun-ws-router')
ESM-only; CommonJS require() fails because package is type:module
WebSocketRouter
import type { WebSocketRouter } from 'bun-ws-router'
import { WebSocketRouter } from 'bun-ws-router'
Use 'import type' for type-only imports to avoid runtime side effects
createRouter (default)
import createRouter from 'bun-ws-router'
import { createRouter } from 'bun-ws-router'
createRouter is the default export, not a named export. Check v0.5+ for renamed default

Creates a WebSocket server with message validation using Zod and pub/sub to 'chat' channel.

import { defineRouter } from 'bun-ws-router'; import { z } from 'zod'; const router = defineRouter({ open: (ws) => { ws.subscribe('chat'); }, message: (ws, message) => { const schema = z.object({ text: z.string() }); const parsed = schema.parse(JSON.parse(message.data)); ws.publish('chat', parsed.text); }, close: (ws) => { ws.unsubscribe('chat'); }, }); Bun.serve({ port: 3000, fetch(req, server) { if (server.upgrade(req)) return undefined; return new Response('Hello!'); }, websocket: router, }); console.log('Server running on ws://localhost:3000');
Debug
Known issues
breakingDefault export renamed from `createRouter` to `defineRouter` in v0.5.0
fix
Update import to `import { defineRouter } from 'bun-ws-router'`
affects: <0.5.0
deprecatedSupport for `@ts-stack/zod` and `@ts-stack/valibot` removed in v0.6.0
fix
Use plain `zod` or `valibot` packages
affects: >=0.6.0
gotchaRouter must be attached to `websocket` property of `Bun.serve` options, not inside `fetch`
fix
Set `websocket: router` on the `Bun.serve` object
affects: >=0.1.0
breakingMinimum Bun version required changed to 1.1.0 in v0.6.0
fix
Upgrade Bun to 1.1.0 or later
affects: >=0.6.0
gotchaSchema validation is mandatory in `message` handler if using Zod/Valibot; omitted schema silently passes unvalidated data
fix
Always provide a schema in message handler options
affects: >=0.1.0
Errors
Common errors & fixes
Error: The module's source code could not be parsed. Are you using a normal import or a re-export?
Using CommonJS `require()` on an ESM-only package
fix
Use `import` syntax instead of `require()`
TypeError: server.upgrade is not a function
Passing router to `fetch` callback instead of `websocket` option
fix
Set `websocket: router` in Bun.serve options, not inside fetch
ZodError: Expected string, received number
Message payload does not match schema (e.g., `text` field is number instead of string)
fix
Ensure client sends data matching the schema defined in the `message` handler
Error: Cannot find module 'zod'
Missing peer dependency `zod` (or `valibot`)
fix
Install zod: `bun add zod` or `npm install zod`
TypeScript errors on `ws.publish`/`ws.subscribe`: Property 'publish' does not exist on type 'WebSocket'
TypeScript not recognizing Bun's extended WebSocket types
fix
Install `@types/bun` and ensure tsconfig includes `"types": ["bun-types"]`
Upgrade
Version history
0.6.1latest on npm
Audit
Dependencies
zodoptionalSchema validation for incoming messages
valibotoptionalAlternative schema validation library
@types/bunoptionalType definitions for Bun APIs
Agent activity
23 hits · last 30 days
node
22
OpenAI (training)
1
Resources
bun-ws-router — npm install bun-ws-router · libregistry