Skip to content

Commit f8e85b6

Browse files
committed
Follow the DB
1 parent 98292e1 commit f8e85b6

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

lib/transports/couchdb.js

+41-4
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ Server.prototype.start = function(callback) {
336336
return callback(er);
337337

338338
if(body.couchdb !== 'Welcome')
339-
retur callback(new Error('Cannot confirm CouchDB server: ' + self.url));
339+
return callback(new Error('Cannot confirm CouchDB server: ' + self.url));
340340

341341
self.log.debug('Confirming CouchDB admin credentials');
342342
self.req('/_session', function(er, resp, body) {
@@ -406,10 +406,47 @@ Server.prototype.prep = function(callback) {
406406

407407
Server.prototype.listen = function(callback) {
408408
var self = this;
409-
self.log.debug('Listening for CouchDB connections');
410409

411-
// TODO
412-
};
410+
if(self.feed)
411+
return callback(new Error('Server feed already exists'));
412+
413+
self.feed = new follow.Feed;
414+
415+
self.feed.db = self.url + self.resource;
416+
self.feed.since = 0;
417+
self.feed.heartbeat = 30 * 1000; // 30 seconds
418+
self.feed.inactivity_ms = 3 * 86400 * 1000; // 3 days
419+
self.feed.include_docs = true;
420+
self.feed.filter = message_filter;
421+
422+
self.feed.on('change', on_change);
423+
self.feed.on('error' , on_error);
424+
425+
self.log.debug('Listening for CouchDB transport connections');
426+
self.feed.follow();
427+
428+
var id_re = /^socket\.io\/1\/([0-9a-f]{32})$/;
429+
var connections = {};
430+
431+
function message_filter(doc, req) {
432+
var match = doc._id.match(id_re);
433+
if(!match)
434+
return false;
435+
436+
var transport_id = doc._id.match(id_re)[1];
437+
438+
return (transport_id in connections) || doc.state === 'opening';
439+
}
440+
441+
function on_error(er) {
442+
self.emit('error', er);
443+
}
444+
445+
function on_change(change) {
446+
self.log.debug('Change ' + change.seq + ' is ' + change.id + ': ' + JSON.stringify(change.doc));
447+
}
448+
}
449+
413450

414451
/**
415452
* Data validator for the public connection DB

0 commit comments

Comments
 (0)