# SocketMiddlewareError Decorator

Module
import { SocketMiddlewareError } from "@tsed/socketio"
Source/packages/socketio/src/decorators/socketMiddlewareError.ts

# Overview

function SocketMiddlewareError(): Function;

# Description

Declare a new SocketMiddlewareError.

# Example

A middleware can be also used on a SocketService either on a class or on a method.

Here an example of a middleware:

import {SocketMiddlewareError, SocketErr, Socket, Args} from "@tsed/socketio";

@SocketMiddlewareError()
export class ErrorHandlerSocketMiddleware {
   async use(@SocketEventName, @SocketErr err: any, @Socket socket: SocketIO.Socket) {
       console.error(err);
       socket.emit("error", {message: "An error has occured"})
   }
}
1
2
3
4
5
6
7
8
9

Then:

import {SocketService, SocketUseAfter, SocketUseBefore, Emit, Input, Args} from "@tsed/socketio";
import {ErrorHandlerSocketMiddleware} from "../middlewares";
import {User} from "../models/User";

@SocketService("/my-namespace")
@SocketUseAfter(ErrorHandlerSocketMiddleware) // global version
export class MySocketService {

   @Input("eventName")
   @Emit("responseEventName") // or Broadcast or BroadcastOthers
   @SocketUseAfter(ErrorHandlerSocketMiddleware)
   async myMethod(@Args(0) userName: User) {

       console.log(user);
       throw new Error("Error");

       return user;
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19