Skip to content

ShokupanWebsocketRouter

Defined in: src/websocket.ts:81

WebSocket Router for organizing WebSocket endpoints.

Provides lifecycle hooks and event-based message routing.

const wsRouter = new ShokupanWebsocketRouter();
wsRouter.onUpgrade((ctx) => {
if (!ctx.get("authorization")) return false;
return true;
});
wsRouter.onOpen((ctx, ws) => {
return { userId: "123" }; // Sets ws.data and ctx.state
});
wsRouter.event("chat.message", (ctx, data) => {
ctx.broadcast("chat.message", data);
});
app.mount('/ws', wsRouter);

T extends Record<string, any> = any

new ShokupanWebsocketRouter<T>(): ShokupanWebsocketRouter<T>

ShokupanWebsocketRouter<T>

middleware: any[] = []

Defined in: src/websocket.ts:84

get registry(): object

Defined in: src/websocket.ts:310

Registry Accessor to support Dashboard Graph

object

controllers: never[] = []

events: any[]

metadata: undefined = undefined

middleware: never[] = []

routers: never[] = []

routes: never[] = []

event(name, handler): this

Defined in: src/websocket.ts:167

Register an event handler.

string

Event name

EventHandler<T>

Handler function

this

router.event("chat.message", (ctx, data) => {
ctx.broadcast("chat.message", data);
});

getRoutes(): any[]

Defined in: src/websocket.ts:303

any[]


mount(prefix, router): this

Defined in: src/websocket.ts:266

Mount a child WebSocket router or controller to share the same connection. Events from the child will be prefixed with the mount path.

string

Event prefix for the child router (e.g., “chat” makes “message” become “chat.message”)

any

Child WebSocket router or controller to mount

this

const mainRouter = new ShokupanWebsocketRouter();
const chatRouter = new ShokupanWebsocketRouter();
chatRouter.event("message", (ctx, data) => { ... });
// Events will be accessible as "chat.message"
mainRouter.mount("chat", chatRouter);

onClose(handler): this

Defined in: src/websocket.ts:131

Register close handler. Called when WebSocket connection is closed.

(ctx, ws, code?, reason?) => void | Promise<void>

this


onError(handler): this

Defined in: src/websocket.ts:140

Register error handler. Called when an error occurs.

(ctx, ws, error) => void | Promise<void>

this


onEvent(handler): this

Defined in: src/websocket.ts:113

Register event middleware handler. Called before routing to specific event handlers. Return false or throw to prevent event routing.

(ctx, ws, event, data) => boolean | void | Promise<boolean | void>

this


onMessage(handler): this

Defined in: src/websocket.ts:122

Register message handler. Called for every message before event parsing/routing.

(ctx, ws, message) => void | Promise<void>

this


onOpen(handler): this

Defined in: src/websocket.ts:103

Register open handler. Called after WebSocket connection is established. Return value is automatically set to both ws.data and ctx.state.

(ctx, ws) => any

this


onStop(handler): this

Defined in: src/websocket.ts:149

Register stop handler. Called when the server is stopped.

(app) => void | Promise<void>

this


onUpgrade(handler): this

Defined in: src/websocket.ts:93

Register upgrade validation handler. Called when HTTP upgrade request is received. Return false to reject the upgrade.

(ctx) => boolean | void | Promise<boolean | void>

this