ChangeStream From MongoDB

27 views
Skip to first unread message

David Simmons

unread,
Jan 28, 2016, 1:02:06 PM1/28/16
to StrongLoop

I'm trying to set up a ChangeStream for data from my various MongoDB Datasources, and having a hard time figuring out exactly how to create the ChangeStream to update when new data is stored in MongoDB

The docs at https://docs.strongloop.com/display/public/LB/Realtime+server-sent+events give a relatively simplistic example of setting up a change stream — it emits one change, and that’s it.
var es = require('event-stream');
module.exports = function(app) {
  var MyModel = app.models.MyModel;
  MyModel.createChangeStream(function(err, changes) {
    changes.pipe(es.stringify()).pipe(process.stdout);
  });
  MyModel.create({foo: 'bar'});
}

There used to be a github example of streaming to a chart, but that is now 404 … https://github.com/strongloop/loopback-example-angular-live-set/tree/feature/init/client/modules/chart

So, any quick info on how to set up a change-stream from a MongoDB API in StrongLoop? 

Having this:
var es = require('event-stream');
module.exports = function(app) {
  var myApp = app.models.Magnetometer;
  myApp.createChangeStream(function(err, changes) {
    changes.pipe(es.stringify()).pipe(process.stdout);
  });
  myApp.create({foo: 'bar'});
}

in my server/boot/realtime.js does indeed emit the ONE foo: “bar” message, but it would be much more helpful to know how to hook that to the MongoDB data that is changing so that it can stream that to the client. :-) 

Thanks!
dg

Kranti K

unread,
Dec 9, 2016, 6:12:08 AM12/9/16
to StrongLoop
Where you able to figure this out? Having similar problem.
Reply all
Reply to author
Forward
0 new messages