Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io + using 'emit' with a callback?

I'm having an issue with my sockets where my server side code is executing before my socket.emit finishes. Here's 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 what I need to have happen is the code that gets executed on the client via socket.emit('GPS', ...) to completely finish before toolbox.drawPaths happens. I tried to toss a callback on the emit like you would socket.on(..., function(){...}) but that doesn't seem to be part of the API. Any suggestions?

like image 879
gjw80 Avatar asked Oct 03 '22 06:10

gjw80


1 Answers

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

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);
  });
});
like image 200
Xinzz Avatar answered Oct 13 '22 11:10

Xinzz