Closed anton-g closed 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.
Great! Thanks for your hard work!
Thanks @anton-g!
@kamilmysliwiec Is it possible that in the v5.0 the docs are missing for this feature?
It's possible now (with custom adapter of course)
@kamilmysliwiec but there is no documentation for it?
@BorntraegerMarc Is this what you're looking for? https://docs.nestjs.com/websockets/adapter
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 :/
@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.
@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)' }
@FranciZ can you post your gateway event and the emitter on the frontend? Are you able to get the repo I posted to work?
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
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.
I'm submitting a...
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:I also posted about this here, but no one seems to have a working solution.
Environment