Socket.io + using 'emit' with callback?

I have a problem with my sockets where my server side code is executed until mine is complete socket.emit. Here is a snippet:

admin.getTh(function(th){
   socket.emit('GPS', {gpsResults: data, thresholds: th, PEMSID: PEMSID, count: count});
   count++;
   toolbox.drawPaths(function(ta){
        console.log(ta);
   });
});

So, I need the code that is executed on the client using socket.emit('GPS', ...)to complete completely before toolbox.drawPaths. I tried sending a request to emit, like you socket.on(..., function(){...}), but this does not seem to be part of the API. Any suggestions?

+4
source share
1 answer

One thing you might want to do is create an event on the server that will be emitted when your GPS is complete.

For example:

Server

socket.on ('GPS', function (data) {
  // Do work

  // Done doing work
  socket.emit ('messageSuccess', data);
});

Client

socket.emit('GPS', {gpsResults: data, thresholds: th, PEMSID: PEMSID, count: count});
socket.on ('messageSuccess', function (data) {
  //do stuff here
  toolbox.drawPaths(function(ta){
    console.log(ta);
  });
});
+4
source

All Articles