Simulate the behavior of `Meteor.status()` with TCP sockets

Has anyone worked with this? I am trying to make a connection that works similar to Meteor’s connection. I have something that looks as follows

// common
Cylon.connections = new Mongo.Collection('cylon_connections');

// server
Cylon.connections._ensureIndex({ name: 1 }, { unique: true });
CylonConnection = function (name) {
  let future = new Future();
  let socket = new net.Socket();
  socket._name = name;

  this.connections.upsert({ name: socket._name }, {
    $set: {
      'connection.status': 'connecting',
      'connection.reason': null,
      'connection.retryCount': 0
    },
    $setOnInsert: {
      messages: []
    }
  });

  socket.addListener('connect', Meteor.bindEnvironment(() => {
    this.connections.update({
      $set: { 'connection.status': 'connected' },
      $unset: { 'connection.reason': null }
    });
    future.return();
  }));
  future.wait();
}

Is this how this is done within meteor? What is the appropriate way to handle it?

I don’t know why you’d need futures when you’re using bindEnvironment. I never worked with cylon, but here’s an example using raw TCP packets (wireless cardiograph for ambulances in mexico) https://github.com/mattkrick/meteor-vital-signs/blob/master/server/sensorServer.js#L7
Hopefully it has something useful, cheers.

I see, what is the point of Meteor.bindEnvironment then?

Meteor runs in a fiber, bindEnvironment puts your context in that fiber. You can still use fibers/futures directly, but since around v0.8ish Meteor’s built-in functions have abstracted away the need for just about every use case. With ES2016 we should be able to finally do away with that stuff & just use generators & await/async, which I’m guessing Meteor will use under the hood to serve the same purpose.