function WebSocketPlugin
Since 0.1.0
WebSocketPlugin(options?: WebSocketPluginOptions): IPlugin

Creates the WebSocketPlugin.

Registers an IWebSocketService under CAPABILITIES.WEBSOCKET. Single instance only — the kernel's plugin resolver throws at startup on a duplicate name or a duplicate capability provider.

The plugin resolves the application's IHttpAdapter and installs its upgrade router there. When the adapter implements no upgrade seam, the service still registers (so one codebase deploys everywhere) but reports available: false and fails route() with a WebSocketUnavailableError.

Examples

Example 1

import { WebSocketPlugin } from '@setu-ts/websocket-plugin';
import { CAPABILITIES, type IWebSocketService } from '@setu-ts/common';

app.register(WebSocketPlugin({ heartbeatMs: 30_000, idleTimeoutMs: 90_000 }));

const ws = app.services.get<IWebSocketService>(CAPABILITIES.WEBSOCKET);
ws.route('/ws/chat', {
  onOpen: (conn) => ws.room('lobby').add(conn),
  onMessage: (conn, data) => ws.room('lobby').broadcast(data, { except: conn }),
});

Parameters

optional
options: WebSocketPluginOptions

Plugin configuration

Return Type

IPlugin

The plugin instance

Throws

Error

If idleTimeoutMs is set without a heartbeatMs to sweep on

Usage

import { WebSocketPlugin } from "websocket-plugin/src/index.ts";