Skip to content

Instantly share code, notes, and snippets.

@cenuij
Last active April 26, 2018 09:49
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 cenuij/cef04e1bf65c44edcbf341fea3ab797e to your computer and use it in GitHub Desktop.
Save cenuij/cef04e1bf65c44edcbf341fea3ab797e to your computer and use it in GitHub Desktop.
Start/stop following CouchDB changes listener
const nano = require("nano")("http://localhost:5984");
const name = "test";
nano.db.destroy(name, () => {
nano.db.create(name, () => {
const db = nano.use(name);
const feed = db.follow({ since: "now" });
// add doc every second
const timer = setInterval(() => {
db.insert({}, (err, body) =>
console.log(err ? "ERROR" : "ADD " + body.id)
);
}, 1000);
const handler = change => console.log("GOT " + change.id);
feed.on("change", handler);
// start feed after 5s
setTimeout(() => feed.start(), 5000);
// stop feed after 10s
setTimeout(() => {
clearInterval(timer);
feed.stop();
}, 10000);
});
});
@cenuij
Copy link
Author

cenuij commented Apr 26, 2018

Running this bombs out with :

$ node startstop.js
ADD ae1b3a77338a73da2f3cb4cf162768e2
ADD ae1b3a77338a73da2f3cb4cf162768e9
ADD ae1b3a77338a73da2f3cb4cf162769cc
ADD ae1b3a77338a73da2f3cb4cf162773b6
ADD ae1b3a77338a73da2f3cb4cf1627757a
GOT ae1b3a77338a73da2f3cb4cf1627757a
ADD ae1b3a77338a73da2f3cb4cf16277c4c
GOT ae1b3a77338a73da2f3cb4cf16277c4c
ADD ae1b3a77338a73da2f3cb4cf16277c60
GOT ae1b3a77338a73da2f3cb4cf16277c60
ADD ae1b3a77338a73da2f3cb4cf16278a7b
GOT ae1b3a77338a73da2f3cb4cf16278a7b
ADD ae1b3a77338a73da2f3cb4cf16278da7
GOT ae1b3a77338a73da2f3cb4cf16278da7
_http_client.js:355
  if (req.res && req.res.readable) {
          ^

TypeError: Cannot read property 'res' of null
    at Socket.socketCloseListener (_http_client.js:355:11)
    at Socket.emit (events.js:180:13)
    at TCP._handle.close [as _onclose] (net.js:541:12)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment