#Does anyone know how to use interceptors with websockets?

1 messages · Page 1 of 1 (latest)

storm crane
#
import {
    Injectable,
    NestInterceptor,
    ExecutionContext,
    CallHandler,
    Scope,
  } from '@nestjs/common';
  import { log, trace } from 'console';
  import { Request} from 'express';
  import { Observable } from 'rxjs';
  import { tap } from 'rxjs/operators';
  import { WinstonLoggerService } from 'src/common/loggers/winston-logger.service';
  
  @Injectable()
  export class LoggingInterceptor implements NestInterceptor {
    private readonly logger = new WinstonLoggerService(LoggingInterceptor.name)
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
      let req: Request = context.switchToHttp().getRequest()

      const message = {
        traceId: req.body.traceId,
        path: req.path,
        method: req.method,
        headers: req.headers,
        body: req.body,
        params: req.params,
        query: req.query
      }
  
      const data = {
        message: JSON.stringify(message)
      }
      
      return next.handle().pipe(tap(() => this.logger.log(data)))
    }
  }

But this code only handles http requests and responses. how do I handle websocket requests and responses?

feral iris
#

please don't use the suggestion to the fw tag unless you have some suggestion to the framework
Also, don't use the NestJS Devtools tag unless your question is related with https://devtools.nestjs.com (note the capital D)

turbid river
#

How is the interceptor being bound and used?

storm crane
turbid river
#

Yeah, global enhancers don't bind for websockets

storm crane
turbid river
#

I think we mention that somewhere, but I'd have to find it again

storm crane
#

we have hundreds of func in the code and we will have to put the decorator manually

#

for each of them

#

@turbid river do you know if there is websock middleware interface somehwere in nest.js? I could not find any

#

unfortunately

turbid river
#

You have hundreds of websocket gateway classes?

#

I'm not aware of Nest having a middleware interface for websockets.ws itself doesn't support it IIRC, so it's only an idea with socket.io. Technically you could make your own adapter that makes use of it, but nothing "official" to my knowledge

storm crane
turbid river
storm crane
#

I mean many classes yeah.

#

but okay makes sense

#

thanks though

#

btw another question

#

if you are still here

#

and if I dont bother you

#

so I have a exception filter class

#

and I want to put different exception classes there in the decorator

#

so in order to let my exeption filter catch all of those types of exception

#

do I need to override the catch function for every exception type

#

?

turbid river
#

How many exception types are you doing this for?

storm crane
#

WsException and HttpException

#

import { ExceptionFilter, Catch, ArgumentsHost, HttpException, Injectable } from '@nestjs/common';
import { WsException } from '@nestjs/websockets';
import { Request, Response } from 'express';

@Catch(HttpException, WsException)
@Injectable()
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) 
  {        
  }
  catch(exception: WsException, host: ArgumentsHost) 
  {        
  }
}
#

can I do this for example?

vestal pelican
# storm crane ```js import { ExceptionFilter, Catch, ArgumentsHost, HttpException, Injectable...

Well, you can't do this exactly, since Typescript still compiles to javascript and javascript doesn't do function overloading like you're used to from other compiled languages. What you can do is this:

@Catch(HttpException, WsException)
@Injectable()
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException | WsException, host: ArgumentsHost) 
  {      
     if (exception instanceof HttpException) {
        return this.catchHttp(exception, host)
     } else {
        return this.catchWs(exception, host)
     }
  }
  catchHttp(exception: WsException, host: ArgumentsHost) 
  { }
  catchWs(exception: WsException, host: ArgumentsHost) 
  { }
}
turbid river
#

Oh crap, didn't even recognize the overload, just saw the catch decorator. Yeah, listen to papooch there

storm crane
storm crane
#

but it gives me this error

vestal pelican
storm crane
#

Oh my bad sorry

#

did not really take a look at it