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

node.js, socket.io, sockets

Solution

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);
  });
});

Problem

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?

Original source