For a log viewer, I have to write the updates in a mongoDB collection on a sockJS socket.
Based on this post, I'm using the stream cursor of the native nodeJS driver to do so, but it only works for the content present in the collection when I create the stream. None of the further updates are written.
Here is my code:
var server = new mongodb.Server(config.db_host, config.db_port, {});
var DB = new mongodb.Db('myLogs', server, {w:0}).open(function (error, database) {
if (error) throw error;
db.logs = database.collection('logs');
});
var stream = db.logs.find({user: sID}, {sort: [['_id', 'asc']]}).stream();
stream.on('error', function (err) {
socket.write(JSON.stringify({'action': 'log','param': 'log db streaming error'}));
});
stream.on('data', function (doc) {
socket.write(JSON.stringify({'action': 'log','param': doc.log}));
});
What am I doing wrong? Can this work?