Runtime-specific websocket routers built for performance.
Example route:
import { autoRoute } from 'ws-routers/bun';
export default autoRoute<{ id: number }>({
message: (ws, msg) => {
ws.send(ws.data.id + ':' + msg);
}
});
Use in a request handler:
import handleSocket from '/path/to/route';
export default (req: Request): Response => {
// Similar to server.upgrade but run the handlers for that route only
if (handleSocket(req, {
id: Math.random()
})) return;
// Do other things...
}
And serve with autoServe
instead of Bun.serve
:
import { autoServe } from 'ws-routers/bun';
autoServe({
fetch: myRequestHandler,
port: 3000,
// Other options...
});
Example route:
import { route } from 'ws-routers/deno';
export default route<{ id: number }>({
message(event) {
// Access the current socket with `this`
// Obtain the data passed in with `this.$`
this.send(this.$.id + ':' + event.data);
}
});
Use in a request handler:
import handleSocket from '/path/to/route';
export default (req: Request): Response => {
if (req.headers.get("upgrade") === "websocket") {
// The websocket is handled by the route
const res = handleSocket(req, {
$: { id: Math.random() }
});
// Other code...
// Then return the socket response
return res.response;
}
// Do other things...
}
Example route:
import { route } from 'ws-routers/cloudflare';
export default route<{ id: number }>({
message(event) {
// Access the current socket with `this`
// Obtain the data passed in with `this.$`
this.send(this.$.id + ':' + event.data);
}
});
Use in a request handler:
import handleSocket from '/path/to/route';
export default (req: Request): Response => {
if (req.headers.get("upgrade") === "websocket") {
// The websocket is handled by the route
const socketPair = handleSocket(req, { id: Math.random() });
// Other code...
// Then return the response with the client socket
return new Response(null, {
status: 101, webSocket: socketPair[0]
});
}
// Do other things...
}
A very simple event model.
import event from 'ws-routers/event';
// Create a topic
const [subscribers, publish] = event((subscriber, ...args) => {
// Do something with subscriber and args...
});
// Set operations
subscribers.add(subscriber);
subscribers.remove(subscriber);
// Publish some data to all subscribers (must match callback args)
publish(...args);