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
66.89k stars 7.56k forks source link

Multiple parameters in @SubscribeMessage functions #581

Closed anton-g closed 6 years ago

anton-g commented 6 years ago

I'm submitting a...


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

Current behavior

It seems that there is no support for socket adapters that pass multiple parameters to the message handler functions. This becomes an issue when trying to use acknowledgement functions in socket.io for example.

According to #249 this should be possible to do with a custom socket adapter, but I can't get it to work. Somewhere along the way to the message handler the second parameter is dropped.

Expected behavior

I would like to be able to do something like this: socket.emit('event', 'some data', function (response) { /*do something*/ }); and then the socket adapter should pass both the data and the function into the message handler function:

@SubscribeMessage('event')
onStart(client, data, ack) {
  //Do stuff
  ack('stuff completed');
}

I also posted about this here, but no one seems to have a working solution.

Environment


Nest version: 4.6.6


For Tooling issues:
- Node version: 8.10.0  
- Platform:  Mac
kamilmysliwiec commented 6 years ago

Hi @anton-g, Yes, you're right, this is a missing feature. You'll be able to do that in v5.0.0 release.

anton-g commented 6 years ago

Great! Thanks for your hard work!

kamilmysliwiec commented 6 years ago

Thanks @anton-g!

BorntraegerMarc commented 6 years ago

@kamilmysliwiec Is it possible that in the v5.0 the docs are missing for this feature?

kamilmysliwiec commented 6 years ago

It's possible now (with custom adapter of course)

BorntraegerMarc commented 6 years ago

@kamilmysliwiec but there is no documentation for it?

josiahdahl commented 6 years ago

@BorntraegerMarc Is this what you're looking for? https://docs.nestjs.com/websockets/adapter

BorntraegerMarc commented 6 years ago

To be honest I'm not sure 😄 The docs seem fairly complicated. I just wanted to have a ack method in @SubscribeMessage. Not sure why a custom adapter is needed :/ it's a pretty straightforward socket.io feature :/

josiahdahl commented 6 years ago

@BorntraegerMarc Here's a solution that works for me - not sure if it's what you need as my experience with websockets is limited.

@WebSocketGateway()
export class EventsGateway {
  @WebSocketServer() server;

  @SubscribeMessage('event')
  onEvent(client, [data, cb]) {
    cb('Custom Data');
  }
}
// Assuming a socket.io instance socket
const socket = io('http://localhost:3001');
// ....
socket.emit('event', 'Some data', (res) => {
  console.log(res);
  // Logs: Custom Data
});

I have a minimal example here https://github.com/josiahdahl/nestjs-ws-callback-example. Clone it, yarn install, yarn run start, then visit localhost:3000 and open your console to see the callback being handled.

FranciZ commented 6 years ago

@josiahdahl Doesn't work for me. I get the below error. If setting the second attribute as array. Any idea what could be the cause?

{ message: 'undefined is not a function',
  stack: 'TypeError: undefined is not a function\n    at GameGateway.onPlayerSeenChat (.../api/src/modules/game/game.gateway.ts:190:25)\n    at WsContextCreator.<anonymous> (.../api/node_modules/@nestjs/websockets/context/ws-context-creator.js:38:33)\n    at Generator.next (<anonymous>)\n    at fulfilled (.../api/node_modules/@nestjs/websockets/context/ws-context-creator.js:4:58)\n    at <anonymous>\n    at process._tickCallback (internal/process/next_tick.js:182:7)' }
josiahdahl commented 6 years ago

@FranciZ can you post your gateway event and the emitter on the frontend? Are you able to get the repo I posted to work?

AndyGura commented 6 years ago

I wrote this module over nestjs, which do it under the hood so you don't need to call ack callback explicitly, you only need to return value or throw error: https://www.npmjs.com/package/nestjs-socket-handlers-with-ack . Hope it helps

lock[bot] commented 4 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.