Skip to main content

WSOperation

Package: @opra/common

WSOperation is the runtime class that represents a single WebSocket operation within a WSController. An operation maps a socket event name (or pattern) to a handler, declares the argument types it accepts, and optionally declares a response type for request-response flows.


Inheritance

DocumentElement
└── WSOperation

Properties

PropertyTypeDescription
namestringOperation name as registered on the controller.
descriptionstring | undefinedHuman-readable description.
eventstring | RegExpThe socket event name or pattern this operation handles. Defaults to the operation name if not explicitly set.
argumentsWSOperationArgument[]Declared argument descriptors — each carries a type: DataType, a parameterIndex: number, and an optional required flag.
typesDataTypeMapLocal data types scoped to this operation.
responseDataType | undefinedExpected response type for request-response flows. undefined for fire-and-forget operations.

WSOperationArgument

PropertyTypeDescription
typeDataTypeData type used to validate the argument at this position.
parameterIndexnumberZero-based index of the argument in the handler's parameter list.
requiredboolean | undefinedWhether the argument must be present.

Methods

toJSON()

Returns a plain OpraSchema.WSOperation object for schema export.

toJSON(): OpraSchema.WSOperation

Event matching

When event is a string it must match exactly. When it is a RegExp, the framework tests incoming event names against the pattern. In decorator usage, a string starting with / is automatically parsed as a regular expression:

@WSOperation({ event: /^room:.+/ })
joinRoom(ctx: SocketioContext, payload: JoinRoomDto) { ... }

Obtaining a WSOperation instance

import { WSApi } from '@opra/common';

const document = await ApiDocumentFactory.createDocument({ ... });

if (document.api instanceof WSApi) {
const op = document.api.findOperation('ChatController', 'sendMessage');

if (op) {
console.log(op.name); // 'sendMessage'
console.log(op.event); // 'sendMessage' or /pattern/
console.log(op.arguments); // [{ type: MessageType, parameterIndex: 0 }]
console.log(op.response?.name); // response type name, if any
}
}

WSController · WSApi