Middlewares
Chain middlewares per socket event
SugoiJS gives the ability to chain middlewares on event level by the
@SocketOn
, @SocketOnByHandler
, @SocketServerOn
and @SocketServerOnByHandler
decoratorsEach of the decorators get as many middlewares as you specify, those middlewares will apply in the same order you passed the.
@SocketOn("message","/",(socket,data)=>{
console.log("log message data: %s from socket id: %s",data,socket.id);
})
function messageHandler(socket,data){
socket.to(data.room).emit('message',data.message)
}
In case value was returned (!= undefined) from the middleware method, this value will be be passed to next middleware and evently to the action method itself.
Since middlewares are sequential, you can use them to verify the data and break the flow whenever is needed.
By using the
BreakMiddleware
methodexport function BreakMiddleware(reason?:any);
reason
- The data which should be added to the exception.@SocketOn("message","/",
(socket,data)=>{
console.log("log message data: %s from socket id: %s",data,socket.id);
},
(socket,data)=>{
if(!data.message)
BreakMiddleware("No message property");
})
function messageHandler(socket,data){
socket.to(data.room).emit('message',data.message)
}