🍡 mochi
SSR framework for Svelte 5 + Bun with islands-based selective hydration
WebSocket routes
Mochi.ws() creates WebSocket endpoints with open, message, close, and drain handlers. Bun’s pub/sub is available via ws.subscribe() and ws.publish():
"/ws/chat": Mochi.ws({
open(ws) {
ws.subscribe("chat");
},
message(ws, message) {
ws.publish("chat", String(message));
ws.send(String(message));
},
close(ws) {
ws.unsubscribe("chat");
},
}),An optional upgrade handler can validate the connection and attach user data:
Mochi.ws<{ userId: string }>({
upgrade(req) {
const userId = req.headers.get('x-user-id');
if (!userId) return false; // reject
return { userId }; // attached to ws.data.user
},
message(ws, msg) {
console.log(ws.data.user.userId, msg);
},
});