Ever built a WebSocket API and felt like you were back to writing raw JSON strings? WS-Kit brings TypeScript type-safety to real-time communication on Bun — schemas flow from server to client automatically, and you get full IDE autocompletion everywhere.
What Makes It Cool
- Type-Safe Routes: Define message schemas once with Zod, get full type inference in handlers
- No Schema Repetition: Use the same validators client and server — pick Zod or Valibot
- RPC Built-In: Request/response patterns with automatic correlation and timeouts
- Fire-and-Forget + Broadcasting: Send to one client or publish to many, all type-checked
- Bun Native: Runs on Bun's WebSocket implementation — zero overhead
Quick Example
import { z, message, rpc, createRouter } from "@ws-kit/zod";
import { serve } from "@ws-kit/bun";
// Type-safe message schemas
const Ping = message("PING", { text: z.string() });
const Pong = message("PONG", { reply: z.string() });
const router = createRouter();
// Fire-and-forget
router.on(Ping, (ctx) => {
ctx.send(Pong, { reply: `Got: ${ctx.payload.text}` });
});
// Or request/response (RPC)
const GetData = rpc("GET", {}, "DATA", { value: z.string() });
router.rpc(GetData, (ctx) => {
ctx.reply({ value: "hello" });
});
serve(router, { port: 3000 });
Connect any WebSocket client and send typed messages — everything is validated automatically.
Docs: https://kriasoft.com/ws-kit/
GitHub: https://github.com/kriasoft/ws-kit <-- give a star 🤩