Tsed: @tsed/socketio bugs

Created on 19 Mar 2018  路  22Comments  路  Source: tsedio/tsed

Informations

I can't import @SocketMiddlewareError() and @SocketMiddleware() decorators (missing import statements).
In examples are @SocketErr() but it should be @SocketErr.

Compilation error (TS version=2.6.2):

node_modules/@tsed/socketio/lib/services/SocketIOServer.d.ts(9,45): error TS1005: '=' expected.

9 export declare const SocketIOServer: unique symbol;

Is there any way to inject to @SocketMiddlewareError() event name on which the error occurred?

Bug | 4.8.1

All 22 comments

@kalu111 Fixed :)

I've added a new decorator SocketEventName. Check the updated documentation ;)

Thanks,
What tag should be now?

v4.9.0

I found an another issue.
Error thrown in middleware is not catched by @SocketMiddlewareError().

Can you also chek it, pls :)

https://github.com/Romakita/ts-express-decorators/blob/22cbf768c4cec843a61641e21ff7d48f08fd2c7b/src/socketio/class/SocketHandlersBuilder.ts#L161

I'm wondering if it is necessary? On every error an application will be stopped?

Normally if you catch the error, the process will not be exited. I don't remember who asked before about that, but is a requested feature. Normally, in Node.js when an error is thrown the process should be down (and free all allocated memory). The developer must put a process manager like PM2 to restart the process automatically.
But I understand the problem with this instruction (because this behavior is not the same with the HTTPException in the express server part).

Normally. If you handle correctly the error (when the fix will be release XD), the catch will not triggered.

Can you send me the configuration of Websocket Service with the event where you have the problem. Because I've tried different use case, and all work fine.

@SocketService('/')
@SocketUseBefore(MySocketMiddleware)
@SocketUseAfter(ErrorHandlerSocketMiddleware)
export class MySocketService {}

@SocketService('/')
export class OtherSocketService {
    @Input('request')
    public async handle() {
        // some code
    }
}

@SocketMiddleware()
export class MySocketMiddleware {
    public async use( ) {
        throw new Error('Error');
    }
}

Thanks

For me it. Works. The ErrorHandlerSocketMiddleware catch the error correctly.

Here my middleware:

@SocketMiddlewareError()
export class ErrorHandlerSocketMiddleware {
    async use(@SocketErr err: any, @Socket socket: SocketIO.Socket) {
        $log.error("===", err);
        socket.emit("eventErr", {message: "An error has occured"});
    }
}

Call sequences:

  • MySocketMiddleware (throw error)
  • OtherSocketService.handler (not called because an error is emitted)
  • ErrorHandlerSocketMiddleware catch error, log it and send response.

How have you declared your ErrorHandlerSocketMiddleware ?

Another issue :)
When I'm injecting @SocketSession to another SocketService or Middleware I'm getting empty Map.

@SocketService('/')
export class MySocketService {
    @Input('request1')
    async handle(@SocketSession session: SocketSession) {
        session.set('test', 'test');
    }
}

@SocketService('/')
export class OtherSocketService {
    @Input('request2')
    async handle(@SocketSession session: SocketSession) {
        const test = session.get('test'); // undefined
    }
}

Pls, check it.

OK, issue with @SocketMiddlewareError resolved :)
I injected a class to the middleware constructor described without @Service() decorator.

Sorry :)

But strange, that there was no warning about that?

But wait ;) I will check it again

yes it's strange. Normally Injector must fail when a Service is unknown

Ok, Socket session isn't shared between two SocketService and I think it's a normal behavior. For example Session in Express is a context for a client on the server side.

Here the session is create for each socket.id and each socket service (whatever the chosen namespace).

You can use you own Service to do that no ?

About @SocketMiddlewareError - I found the issue.
I set the error handler for SocketService where there was no event handlers.

@SocketService('/')
@SocketUseBefore(MySocketMiddleware)
@SocketUseAfter(ErrorHandlerSocketMiddleware)
export class MySocketService {}

When I set error handler to SocketService with event handlter, then it works correctly

@SocketService('/')
@SocketUseBefore(MySocketMiddleware)
@SocketUseAfter(ErrorHandlerSocketMiddleware)
export class OtherSocketService {
    @Input('request')
    public async handle() {
        // some code
    }
}

I was thought that middlewares are inherited :)

About @SocketSession
Yes, I can use my own implementation to store session data between SocketServices.
But I think, when we have opportunity to create multiple SocketService with the same namespace so it would be seems to can use session between them. SocketId is not change until disconnect - so access to the session data is not a problem. Right?

Eventually store session per socket namespace?

I mean #namespace.#socketId=data
It should be grouped :)

Per namespace, I thinks it's possible.

Next release will let you to use the session per namespace :)

v4.10.0

It works great !
Thank you for your time :)

I have also a few new topics but I will create separate threads for that :)

Thanks @kalu111 :)
yes I'll prefer a separate topic for each demand. Is easiest for me to track the issue :)

Was this page helpful?
0 / 5 - 0 ratings