Skip to content

Instantly share code, notes, and snippets.

@nnarhinen
Last active December 21, 2015 22:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nnarhinen/6374611 to your computer and use it in GitHub Desktop.
Save nnarhinen/6374611 to your computer and use it in GitHub Desktop.
Automatically keep your backbone models up-to-date via web sockets by listening to couchdb changes feed
var db = new (require('cradle').Connection)().database('my_db');
db.info(function(err, result) {
var seq = result.update_seq;
db.changes({since: seq, include_docs: true}).on('change', function(change) {
if (change.doc && change.doc.owner) {
io.sockets.in(change.doc.owner).emit('change:' + change.doc.type, decorate(change.doc));
}
});
});
var SomeModel = Backbone.Model.extend({
initialize: function() {
mySocket.on('change:some_type', function(data) {
if (data.id != this.id) return;
this.attributes = data;
this.trigger('change');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment