nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

dynamic namespace can not receive message. #1969

Closed db2crush closed 5 years ago

db2crush commented 5 years ago

I'm submitting a...


[ ] Regression 
[v] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

dynamic WebSocketGateway can not receive message. I set two modules, user and room. Then, web-client send socketio message to namespace /group then only first module in array of modules in app (in this case, UserModule) can receive message. the function fn(data){} in UserGateway receive message only.

When the interface value of WebSocketGateway is string, it works well. but RegExp, it is not work. namespace?: string | RegExp;

Server side code

user.gateway.ts @.WebSocketGateway({ namespace: /group/, }) @.SubscribeMessage('ABC') fn(data) {} export class UserGateway {}

user.module.ts @.Module({ providers: [UserGateway], }) export class UserModule {}

room.gateway.ts @.WebSocketGateway({ namespace: /group/, }) @.SubscribeMessage('ABC') fn(data) {} export class RoomGateway {}

room.module.ts @.Module({ providers: [RoomGateway], }) export class RoomModule {}

app.module.ts @.Module({ imports: [UserModule, RoomModule], controllers: [AppController], providers: [AppService], }) export class AppModule {}

client side code

const socket = io('/group') socket.emit('ABC', {} , function(res){})

Expected behavior

dynamic WebSocketGateway can receive message. All gateways of modules in app module array can receive message.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: X.Y.Z


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

kamilmysliwiec commented 5 years ago

Please, provide a minimal repository which reproduces your issue.

db2crush commented 5 years ago

https://github.com/db2crush/nestjs-test.git

please check html file.

thx :)

kamilmysliwiec commented 5 years ago

Thank you! I tested this and you're right. Nonetheless, I'm afraid that this issue can rather come from the underlying socket.io library. Also, regardless of the issue, I'm not sure if that is a good idea to create 2 different gateways under 1 namespace + subscribe to exact same messages from within them. You basically can never be sure which method will be evaluated first (because it depends on modules hierarchy)

db2crush commented 5 years ago

thx for fast reply.

how you think about if the event name is different? ex) ‘ABC’ and ‘ABCD’ events on test repo.

These work well when String is used for namespace value.. aka namespace: ‘group’. not regExp

kamilmysliwiec commented 5 years ago

This is due to the collision between UserGateway and RoomGateway. Basically, UserGateway overrides subscribers defined in the RoomGateway. Once you remove UserGateway, your ABCD event will be handled properly.

db2crush commented 5 years ago

Is it the intent of nestjs that the gateway is not part of the module architecture? Http request can receive messages separated by a controller and I thought it was a modular architecture. I wanted to separate the events. I guess I know the intentions of nestjs anyway.

thx for nice lib :)

db2crush commented 5 years ago

one more question,

I have tried to do a namespace dynamic binding with a single gateway, but @.Websocketserver decorator is not dynamic binding to namespace. Is this right?

And.. Should be the @Websocketserver decorator interface be a socketio { namespace }? The { Server } in Socket.io was grabbed as a decorator's interface in example, but the actual output is a some namespace of Socket.io...

kamilmysliwiec commented 5 years ago

And.. Should be the @Websocketserver decorator interface be a socketio { namespace }? The { Server } in Socket.io was grabbed as a decorator's interface in example, but the actual output is a some namespace of Socket.io...

see my answer here https://github.com/nestjs/nest/pull/1981

lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.