Skip to content

WsParamsFactory

reference
1 min readUpdated

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

MethodSignatureReturns
exchangeKeyForValue`exchangeKeyForValue(type: number, data: stringundefined, args: unknown[])`

Where it refuses work

  • WsParamsFactory stops the work with an early return when !args.

Diagram

mermaid
graph 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

ts
import { 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 and args[1] as the incoming event payload.
  • Use WsParamtype.SOCKET to resolve the client connection and WsParamtype.PAYLOAD to 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?

Download as PDF
WsParamsFactory — NestJS head-to-head