Kind: Interface
Source: packages/websockets/adapters/ws-adapter.ts
Part of: Websockets
BaseWsInstance defines the minimal WebSocket-like contract required by the WS adapter. Implementations must support event subscription through on and expose a close function so the adapter can listen for lifecycle events and terminate connections consistently.
Properties
| Property | Type |
|---|---|
on | (event: string, callback: Function) => void |
close | Function |
Diagram
mermaidgraph LR Adapter[WS Adapter] --> Instance[BaseWsInstance] Instance --> On["on(event, callback)"] Instance --> Close["close()"] On --> Events[Connection and message events] Close --> Cleanup[Connection cleanup]
Usage
tsimport type { BaseWsInstance } from './ws-adapter';
function registerConnection(socket: BaseWsInstance) {
socket.on('message', (data: unknown) => {
console.log('Received message:', data);
});
socket.on('close', () => {
console.log('Connection closed');
});
// Close the connection when it is no longer needed.
socket.close();
}
AI Coding Instructions
- Treat
BaseWsInstanceas an adapter boundary; only rely ononandclosewhen writing transport-agnostic WebSocket code. - Register event listeners with the expected event names supported by the underlying WebSocket implementation.
- Ensure
closeis called during shutdown, error handling, or resource cleanup paths. - Avoid depending on implementation-specific socket APIs unless the adapter interface is explicitly extended.
Relationships
- IMPORTS →
INestApplicationContext - IMPORTS →
WebSocketAdapter - IMPORTS →
WsMessageHandler - IMPORTS →
isFunction - IMPORTS →
NestApplication
Was this page helpful?