Setu-TS
    Preparing search index...

    Interface IWebSocketService

    Service contract for the WebSocket hub — registered by the WebSocketPlugin under CAPABILITIES.WEBSOCKET.

    import { CAPABILITIES } from '@setu-ts/common';

    const ws = ctx.services.get<IWebSocketService>(CAPABILITIES.WEBSOCKET);

    ws.route('/ws/chat', {
    onOpen: (conn, { query }) => {
    conn.data.set('room', query.room ?? 'lobby');
    ws.room(query.room ?? 'lobby').add(conn);
    },
    onMessage: (conn, data) => {
    const room = conn.data.get('room');
    if (typeof room === 'string') {
    ws.room(room).broadcast(data, { except: conn });
    }
    },
    });

    0.2.0

    interface IWebSocketService {
        available: boolean;
        connectionCount: number;
        roomCount: number;
        peek(name: string): WebSocketRoom | undefined;
        room(name: string): WebSocketRoom;
        route(
            path: string,
            handlers: WebSocketHandlers,
            options?: WebSocketRouteOptions,
        ): void;
        routeUpgrade?(
            request: Request,
            principal?: IPrincipal,
        ): Promise<WebSocketUpgradeDecision | null>;
    }

    Implemented by

    Index
    available: boolean

    Whether the underlying HTTP adapter can perform WebSocket upgrades.

    connectionCount: number

    Current number of open connections across all routes.

    roomCount: number

    Current number of live rooms.

    • Returns the named room if one already exists, without creating it.

      The non-allocating counterpart to IWebSocketService.room. A presence or dashboard endpoint that reports size for a name taken from a request must use this: room(name) registers one room per distinct name polled, and a room nobody has joined is reclaimed only when some other connection disconnects — so an idle application never reclaims them.

      Parameters

      • name: string

        Room name

      Returns WebSocketRoom | undefined

      The room, or undefined when no room of that name exists

      const present = ws.peek(`board:${boardId}`)?.size ?? 0;
      

      0.4.0

    • Consults the internal upgrade router for an inbound request. Used by the kernel terminal handler to decide whether to upgrade after the middleware pipeline runs.

      Parameters

      • request: Request

        The native, undisturbed upgrade request

      • Optionalprincipal: IPrincipal

        The authenticated principal, when the middleware pipeline produced one (ctx.request.user). Absent when the upgrade was not authenticated; implementations must treat a missing principal as anonymous and must not throw on its absence.

      Returns Promise<WebSocketUpgradeDecision | null>

      The decision, or null to fall through

      0.3.0