Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
SignalR intercept every incoming message (client side)

I have a question regarding SignalR on the clientside (JS). Is there a way to intercept incoming messages at the clientside? I am working on a chat application with a lot traffic on the socket. I want to be able to execute code (catch errors if any) on every incoming event.

Looking at the docs I can't find any existing solution for this. Is there one?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

After a bit of research, this does not seem to be possible.

My first thought for a workaround was using a function to 'pipe' the callback functions through first.

E.G.

  private pipeSignal(cb: (arg: any) => void): (arg: any) => void {
    const errors = checkForErrors();
    if(errors){
      return () => {
        // Handle error here
      };
    }
    return cb;
  }

I then thought setting up the handlers could look as follows:

this.hubConnection.on(
  'MyMethod1',
  this.pipeSignal((method1Val) => {
    console.log(method1Val);
  })
);

this.hubConnection.on(
  'MyMethod2',
  this.pipeSignal((method2Val) => {
    console.log(method2Val);
  })
);

BUT, this results in the 'pipeSignal' function being called only once when setting up the event listener. Thereafter, it will not be called.

I think the only option here would be to have a function that checks for errors in every event listener callback.

   private hasErrors(arg: any): boolean {
     const errors = checkForErrors();
     if(errors){
         // Handle error here
         return true;
     }
     return false;
   }

Then in each signal event callback you can try checking for errors like this:

this.hubConnection.on(
  'MyMethod1',
  (method1Val) => {
     if(!this.hasErrors(method1Val)){ 
       // Can continue knowing there are no errors
       console.log(method1Val);
     }
  }
);

UPDATE

Turns out I was close with my 'pipeSignal' function. Rather than running the error logic in the pipeSignal function, I return a function that wraps the error logic and the callback:

  private pipeSignal(cb: (arg: any) => void): (arg: any) => void {
    return (arg) => {
      const errors = checkForErrors();
      if(errors){
          // handle errors
      } else {
         // proceed with callback as there are no errors
         cb(arg);
      }
    };
  }

Now this can be used as I imagined before:

this.hubConnection.on(
  'MyMethod1',
  this.pipeSignal((method1Val) => {
    console.log(method1Val);
  })
);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda