@bit-js/bun-utils
v1.0.3
Published
An utilities library for Bun.
Downloads
15
Readme
bun-utils
An utilities library for Bun.
WebSocket router
A WebSocket router for Bun.
import { ws } from '@bit-js/bun-utils';
To create a route.
const route = ws.route<{ id: string }>({
message(ws) {
ws.send(ws.data.id);
}
})
Use in a request handler.
function fetch(req: Request) {
return route.upgrade(req, {
data: { id: performance.now() }
});
}
Serve the fetch handler (this step creates the server and bind all routes).
const server = ws.serve({
// Bun.serve options
server: { fetch },
// Bun WebSocket options
ws: { publishToSelf: true }
});
Or with an already created server instance without ws.serve
.
ws.bind(server);