Kind: Class
Source: packages/websockets/factories/ws-params-factory.ts
Part of: Websockets
WsParamsFactory resolves WebSocket handler parameter values from the runtime argument array. It maps WebSocket parameter types—such as the connected socket or message payload—to the values injected into gateway handler methods.
Methods
| Method | Signature | Returns |
|---|---|---|
exchangeKeyForValue | `exchangeKeyForValue(type: number, data: string | undefined, args: unknown[])` |
Where it refuses work
WsParamsFactorystops the work with an early return when!args.
Diagram
mermaidgraph LR A[WebSocket event arguments] --> B[WsParamsFactory.exchangeKeyForValue] B --> C{WsParamtype} C -->|SOCKET| D[Socket client: args[0]] C -->|PAYLOAD| E[Message payload: args[1]] E -->|Property key provided| F[Payload property]
Usage
tsimport { WsParamsFactory } from '@nestjs/websockets/factories/ws-params-factory';
import { WsParamtype } from '@nestjs/websockets/enums/ws-paramtype.enum';
const paramsFactory = new WsParamsFactory();
const client = { id: 'socket-123' };
const payload = { roomId: 'general', message: 'Hello' };
const args = [client, payload];
const socket = paramsFactory.exchangeKeyForValue(
WsParamtype.SOCKET,
undefined,
args,
);
const message = paramsFactory.exchangeKeyForValue(
WsParamtype.PAYLOAD,
'message',
args,
);
console.log(socket); // { id: 'socket-123' }
console.log(message); // "Hello"
AI Coding Instructions
- Treat
args[0]as the WebSocket client andargs[1]as the incoming event payload. - Use
WsParamtype.SOCKETto resolve the client connection andWsParamtype.PAYLOADto resolve the full payload or a payload property. - Pass a payload property key only when extracting a nested top-level value; omit it to receive the complete payload.
- Keep parameter-resolution behavior aligned with WebSocket decorators such as
@ConnectedSocket()and@MessageBody(). - Avoid depending on this factory as a general-purpose object accessor; it is intended for WebSocket handler argument binding.
Relationships
- IMPORTS →
isFunction
Was this page helpful?