Message Decorator
The message decorator enables you to listen to messages under a specific event. You can also specify a validation schema of how the message should be and it will automatically validate for you.
Usage
typescript
const messageSchema = z.object({
foo: z.string().min(1, 'Foo is required'),
});
@Namespace('/foo') // register the namespace
@Controller('/api/playground')
@Middleware(FirstMiddleware)
export default class DummyController {
@Message({ event: 'foo' })
public async onMessage(incomingMessage: unknown, peer: { id: string; ip: string; }): Promise<Record<string, string>> {
// no schema validation, any messages under "foo" will get here
}
@Message({ event: 'bar', validationSchema: messageSchema })
public async onMessage(incomingMessage: unknown, peer: { id: string; ip: string; }): Promise<Record<string, string>> {
// with schema validation, only properly structured messages under "bar" will get here
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
On your client side, messages should be emitted following this pattern:
typescript
websocket.send(
JSON.stringify({
event: 'foo',
data: {
test: 'ok'
}
})
);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
See Also
- Websocket Plugin - Documentation of the Websocket Plugin
- Namespace Decorator - Documentation of the Namespace decorator
- OnConnectionAttempt Decorator - Documentation of the OnConnectionAttempt decorator
- Emit Decorator - Documentation of the Emit decorator
- Broadcast Decorator - Documentation of the Broadcast decorator
- BroadcastOthers Decorator - Documentation of the BroadcastOthers decorator