Receive confirmation on send / emit server

In the socket.io confirmation example, we see that the send / emit client is called back with a server response. Is the same functionality available in the opposite direction - for example, how does the server confirm the reception of the client for sending / emitting from the server? It would be nice to have a send / emit callback even to confirm the success of the receive. I haven’t seen this functionality registered anywhere ... Thanks!

+4
source share
1 answer

Looking at source.io source I found that ACKs are indeed supported for messages sent by the server (but not in transmissions!) (Lines 115-123 from socket.io/lib/socket.js):

if ('function' == typeof args[args.length - 1]) { if (this._rooms || (this.flags && this.flags.broadcast)) { throw new Error('Callbacks are not supported when broadcasting'); } debug('emitting packet with ack id %d', this.nsp.ids); this.acks[this.nsp.ids] = args.pop(); packet.id = this.nsp.ids++; } 

An example of how ack should work (not tested):

 // server-side: io.on('msg', (data, ackCallback) => { console.log('data from client', data); ackCallback('roger roger'); }); // client-side: socket.emit('msg', someData, (answer) => { console.log('server\ acknowledgement:', answer); }); 
+1
source

Source: https://habr.com/ru/post/1399800/


All Articles