question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple parameters in @SubscribeMessage functions

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
josiahdahlcommented, May 15, 2018

@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.

2reactions
AndyGuracommented, Jul 2, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the @nestjs/websockets.SubscribeMessage ...
To help you get started, we've selected a few @nestjs/websockets.SubscribeMessage examples, based on popular ways it is used in public projects.
Read more >
Gateways | NestJS - A progressive Node.js framework
@SubscribeMessage('events') handleEvent(client: Socket, data: string): string ... In the example above, the handleEvent() function takes two arguments.
Read more >
Socket.io acknowledgement in Nest.js
If the socket provider passes multiple arguments into the SubscribeMessage handler the request parameter will be an array with these ...
Read more >
Subscribe and Unsubscribe from a Message Channel
In the component's JavaScript, import the message service features required ... The subscribe() method takes three required parameters, the message context, ...
Read more >
Paho Python MQTT Client-Subscribe With Examples
The diagram below illustrates the subscribe message flow. The subscribe method accepts 2 parameters – A topic or topics and a QOS (quality ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found