Skip to content

Instantly share code, notes, and snippets.

@suprememoocow
Last active December 17, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suprememoocow/5621437 to your computer and use it in GitHub Desktop.
Save suprememoocow/5621437 to your computer and use it in GitHub Desktop.
// This method will send realtime notifications to the faye clients
function notifyClients(method, model, url) {
bayeux.getClient().publish(url, {
method: method,
body: model.toJSON()
});
}
// Attach events to the mongoose schema. Since the default mongoose middleware makes it difficult to
// distinguish between a create and an update event, we use a small utility that helps us to do that.
mongooseUtils.attachListenersToSchema(todoSchema, {
onCreate: function(model, next) {
notifyClients('POST', model, '/api/todos');
next();
},
onUpdate: function(model, next) {
notifyClients('PUT', model, '/api/todos');
next();
},
onRemove: function(model) {
notifyClients('DELETE', model, '/api/todos');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment