Skip to content

WebSocket Types

This file defines the core TypeScript types used for configuring and managing WebSocket (WS) connections in the @sodacore/core WebSocket module. These types provide strong typing for plugin/service configuration and the server WebSocket context.


Types

IConfig

Configuration options for the WebSocket plugin/service.

typescript
export type IConfig = {
	path?: string | string[],                // WebSocket endpoint path(s). Default: '/ws'.
	keepAlive?: boolean,                     // Whether to keep connections alive. Default: true.
	idleTimeout?: number,                    // Idle timeout in seconds. Default: false (no timeout).
	backpressureLimit?: number,              // Backpressure limit in bytes. Default: 1MB (1024 * 1024).
	maxPayloadLength?: number,               // Maximum payload length in bytes. Default: 16MB (1024 * 1024 * 16).
	closeOnBackpressureLimit?: boolean,      // Close connection on backpressure limit. Default: false.
	publishToSelf?: boolean,                 // Whether to publish messages to the sender. Default: false.
	perMessageDeflate?: unknown,             // Compression options (if supported). Default: false.
};

IServerWsContext

Extends the HTTP server context to include the HTTP context for WebSocket connections.

typescript
export type IServerWsContext = IServerContext & {
	httpContext: HttpContext,                // The associated HTTP context for the WebSocket connection
};

Usage

Import and use these types to strongly type your WebSocket plugin or service configuration and context:

typescript
import type { IConfig, IServerWsContext } from './types';

const wsConfig: IConfig = {
	path: ['/ws', '/realtime'],
	keepAlive: true,
	maxPayloadLength: 1024 * 1024 * 8, // 8MB
};

function handleConnection(context: IServerWsContext) {
	const httpCtx = context.httpContext;
	// Access HTTP request data, cookies, etc.
}

Notes

  • IConfig allows fine-tuning of WebSocket server behavior, including paths, payload limits, and connection management.
  • IServerWsContext ensures that each WebSocket connection has access to its originating HTTP context for unified request handling.

Released under the Apache-2.0 License.