5

I am using Socket.io, and I am wondering if there is a good way to use the request / response pattern. Is the best way to do this to use a UUID for each request, and then only handle data from a response that contains that UUID? That's perhaps not the most scalable way to do things.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

7

Socket.io has the option to send an acknowledgement back. It's possible to include data into this callback (see here). I use it as follows:

Server side:

socket.on('event', function(msg, callback) {
 console.log('event received: '+msg);
 callback("Here could be your data");
});

Client side:

socket.emit('event', "my data", function(callbackData){
    console.log('Callback data:', callbackData);
});
pstiegele
  • 116
  • 4
  • yeah thanks, in this case, I am actually looking to get confirmation that the server received the message from the client, not the other way around. – Alexander Mills Oct 22 '17 at 07:50