Last active
December 17, 2015 13:59
-
-
Save suprememoocow/5621437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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