Search code examples
couchdblong-polling

CouchDB longpoll without _changes


I want to use the longpoll option in couchDB, but without the _changes view. The database has this view:

function (doc) {
  if(doc.job)
    emit(doc._id, 1);
}

So it emits all documents that have a tag “job” in them. Note that I have a bunch of other stuff in this DB. Now I want to use a longpoll where the connection stays open as long as there are no documents in the DB with the job tag. As soon as there is a document with the job tag, the db send it to the client and the connection should be closed. The client can now do the job, send a delete command to the db to delete the job, and start listening again. So my idea is to call the view somehow like this:

http://mycouch/mydb/_design/visualize/_view/get_jobs?feed=longpoll&include_docs=true

However, it seems it is only possible to use longpoll with the _changes view. Any workaround?

Thanks for any help


Solution

  • _changes is the feed you need to use in your case. Yo can use it filtering the feed by receiving changes only for "job" tagged documents. You can do this using your view map function.

    http://mycouch/_changes?filter=_view&view=visualize/get_jobs&feed=logpoll&include_docs=true

    You'll receive through this feed any update (create/update/delete) over "job" tagged docs in your database.