#How do I apply interceptors to emit()?

3 messages · Page 1 of 1 (latest)

wheat mulch
#

Hi,
I try to add @UseInterceptors(ClassSerializerInterceptor) on my WebSocketGateway but when I did client.emit('newMessage', message); I still get the password from the users (author & reciptient).

message :
{
  author: User,
  recipient: User,
  text: string,
}```
I conclude that the interceptor only works on return.
If this is true, do I have to manually filter the data I emit, or is there another solution?
If not, how can I use it on emit?
dry pivot
#

You are correct, enhancers (guards, interceptors pipes) only apply on the request-response side of the system (controllers, resolvers, ...).
What you can do on the emit side is push the payload manually through class-tramsformer's "toPlain" method, which is essentially what the class serializer interceptor does.

wheat mulch
#

Thanks, I'll give it a try.