Using futures to read from a socket


Let’s say I have a socket, which will have data written to it. It might output messages like this.

writing names
doing x y and z
I love cats

What I am wondering is, what is the appropriate way to handle this with Futures? Here’s kind of what I have.

let socket = net.connect(connection);
let listenForMessage = function (socket, message, callback) {
  let future = new Future();

  let listenFunc = Meteor.bindEnvironment(function (future, message, data) {
    let resolver = future.resolver();
    // do something with data
    if (message.test(data.toString('ascii'))) {
  }.bind(this, future, message));

  future.resolve(() => {
    socket.removeListener('data', listener);
  socket.addListener('data', listener);


I’m not sure if this is the right way to do it. The alternative is some kind of observer pattern.

let socket = net.connect(connection);
let messages = new Mongo.Collection('_messages');
socket.addListener('data', function (data) {
    addedAt: new Date,
    message: data.toString('ascii')

  added: function () { /**do something with data*/ }

What’s the most “meteoric” way to handle this?