13

Imagine if i had a mongodb with some documents and a nodejs server which emits json data from the mongodb to the client using socket.io, and some process has updated that mongodb document that is currently in the client view, i was wondering if there is a way so that the mongodb notifies the nodejs server when the object gets updated by anyone other than the client himself so that i could emit the updated document json through the open socket, is such thing possible?

i have tried to find some resources on google with no luck.

Kanka
  • 317
  • 1
  • 4
  • 13
  • Do you have control of the process updating MongoDB? – srquinn Jan 16 '14 at 19:25
  • @jibsales yes but its a different server, and i know how to do things manually but i was wondering if built in things could help – Kanka Jan 16 '14 at 19:46
  • No native support for hooks, triggers, listeners, etc but check the following SO post (the one with the most upvotes, not the accepted answer): http://stackoverflow.com/questions/9691316/how-to-listen-for-changes-to-a-mongodb-collection – srquinn Jan 16 '14 at 20:11
  • Oh, you might also be interested in `watch(1)` by the great TJ Hallowaychuck for watching the oplog for changes – https://github.com/visionmedia/watch – srquinn Jan 16 '14 at 20:12
  • @jibsales chek this one https://github.com/TorchlightSoftware/mongo-watch, its just the perfect fit, does all i need even more with the oplog – Kanka Jan 16 '14 at 20:50
  • – awesome!!! Thanks for turning me on to that. Should be real handy! – srquinn Jan 16 '14 at 23:41
  • Uggh- almost perfect until I saw the unholy .coffee extension. When will programmers give up on eliminating a few key strokes in place of dividing the community and slowing things down with precompilation? It's not like JavaScript/prototypical inheritance is rocket science – srquinn Jan 16 '14 at 23:46
  • @jibsales i really hate coffe too more than you do, will convert it to javascript as soon as i wake up, there is an online converter http://js2coffee.org/ – Kanka Jan 17 '14 at 00:07
  • Hahaha – take my word for it, NO ONE hates coffee script more than me. I've been known to get kicked out of forums and blocked from blogs for my very opinionated rants on the topic ;) – srquinn Jan 17 '14 at 01:51
  • 1
    Thanks Kanka and @jibsales for your suggestions, can you guys post answers with this information to help other folks find these solutions? Upvotes await =) – Sean Connolly May 13 '14 at 17:00
  • Meteor does just that for you – malix Feb 18 '16 at 16:26

3 Answers3

4

Say this is a document schema

'use strict';

import mongoose from 'mongoose';

var MessageSchema = new mongoose.Schema({
  toid: String,
  fromid: String,
  toname: String,
  fromname: String,
  content: String,
  date: { type: Date, default: Date.now }
});

export default mongoose.model('Message', MessageSchema);

And here is the backend code where where you change the contents of document

import {Router} from 'express';
var router = new Router();
router.post('/create/:id', function(req, res){
return Message.create(req.body)
    .then(respondWithResultAndEmitAnEvent(res, 201))
    .catch(handleError(res));
}); 

So this respondWithResultAndEmitAnEvent function look something like this, as the name suggest where even a user POST a data to server (say on the url xyz.com/api/create/:id ) the server will emit an event that can be captured by other clients.

function respondWithResultAndEmitAnEvent(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      socket.emit('message_added', entity);
      res.status(statusCode).json(entity);
    }
  };
}

On the client side you can listen to this event "message_added" and update it.

    socket.on('message_added', function ( data ) {
      console.log(data);
      //update the client with new data;
    }); 
Harsh Gupta
  • 444
  • 3
  • 9
0

If you have control of the other process, than I would recommend doing it proactively, meaning that the process of saving is what triggers the code that is "watching" for changes. Otherwise, you could do interval polling, but that is usually inefficient, and generally bad design. You could also look into some of the more interesting methods, such as watching the Mongo Oplog.

Chandler Freeman
  • 899
  • 1
  • 10
  • 25
0

Mongoose has a middlawre: http://mongoosejs.com/docs/middleware.html

You can run a script on save and deleted records. in this script you can do anything including notifying your notifications server.

O_Z
  • 1,515
  • 9
  • 11