nestia icon indicating copy to clipboard operation
nestia copied to clipboard

Add a decorator to handle @MessageBody of NestJS gateways

Open kevpug opened this issue 2 years ago • 2 comments

A decorator for NestJS Gateways

I was about to pull the trigger to replace my entire validation with Nestia/Typia but didn't because I saw there was no gateway @MessageBody decorator replacement.

Would it be possible to add one ?

kevpug avatar Aug 23 '23 17:08 kevpug

I don't have insight about this subject. Can you explain me more detaily?

In my case, when websocket being required, I used this library instead.

https://tgrid.com/en

samchon avatar Aug 27 '23 05:08 samchon

I'm referring to this: NestJS Gateway In brief, WebSockets in Nest.js utilize Gateways, marked with @WebSocketGateway(), to facilitate real-time communication. Gateways manage incoming messages using decorators such as @SubscribeMessage(). The @MessageBody() decorator extracts data from incoming messages. These gateways can respond in sync or asynchronously, while lifecycle hooks oversee initialization, connections, and disconnections.

Considering that Nestia Core provides access to essential decorators for safeguarding and validating data within controller routes, could it also potentially introduce a new decorator dedicated to NestJS Gateways? Specifically, a decorator that replaces the existing @MessageBody()? @MessageBody

The logic might resemble the TypedBody decorator, but adjusted to throw a WSException instead of the standard HTTP bad request error. Moreover, it would require utilizing something like context.switchToWs().getData() to retrieve and validate the client's message.

kevpug avatar Aug 27 '23 19:08 kevpug