couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Garren Smith <gar...@apache.org>
Subject Re: _changes heartbeat not working via ajax call
Date Thu, 11 Dec 2014 10:03:46 GMT
Hi Mike and Dale,

Thanks for the help. timeout=3000 is exactly what I was looking for.

Cheers
Garren

> On 11 Dec 2014, at 11:55 AM, Dale Harvey <dale@arandomurl.com> wrote:
> 
> heartbeat isnt used to complete the connection, its used for a continuous
> connection just to keep it alive, you are looking for timeout=3000, you
> dont 'need' to use eventsource, although it is nicer
> 
> On 11 December 2014 at 10:38, Mike Marino <mmarino@gmail.com> wrote:
>> 
>> Hi Garren,
>> 
>> Your Ajax call won't return any data until it has finished/succeeded.  This
>> doesn't play well with the changes feed, which keeps the connection open
>> and is streaming data.
>> 
>> Can you use the eventsource protocol instead?  (If not, see [1].)  This
>> built-in library does a lot of the work for you so you don't have to worry
>> about manually reconnecting, etc.  See
>> 
>> https://couchdb.readthedocs.org/en/1536-feature_add-docs/changes.html#event-source
>> 
>> [1] Older versions of CouchDB don't support eventsource.  We had to update
>> our code e.g. to run on Cloudant (they don't yet support this protocol for
>> the changes feed) and came up with a hack solution, which attaches a
>> progress listener to the underlying XMLHttpRequest in the ajax call.  This
>> is not ideal for a few reasons, one being that there is no way to clear the
>> incoming buffer even after the changes are consumed.  If you are interested
>> here and this is the route you want to go, I can send you the code.
>> 
>> Cheers,
>> Mike
>> 
>> On Thu, Dec 11, 2014 at 10:27 AM, Garren Smith <garren@apache.org> wrote:
>> 
>>> Hi All,
>>> 
>>> I’m having an issue with monitoring the changes feed of a database on
>>> Couchdb 1.6. I’m using jQuery to monitor the changes feed of a database.
>>> I’ve set it to feed type to longpoll and I’ve set a hearbeat of 3
>> seconds.
>>> I want the request to return after 3 seconds if no changes happened on
>> the
>>> document. This works perfectly via curl using this:
>>> 
>>> curl '
>>> 
>> http://127.0.0.1:5984/MY_DB/_changes?feed=longpoll&since=now&heartbeat=3000&include_docs=true
>> '
>>> <
>>> 
>> http://127.0.0.1:5984/MY_DB/_changes?feed=longpoll&since=now&heartbeat=3000&include_docs=true
>>> '>
>>> 
>>> But if I try and do the exact same via jQuery it never returns. Any ideas
>>> on why it doesn’t and what I’m doing wrong? Here are steps to reproduce
>>> with jQuery:
>>> 
>>> Ppen fauxton in browser: http://localhost:5984/_utils/fauxton/ <
>>> http://localhost:5984/_utils/fauxton/>
>>> Open the dev console.
>>> Run this code snippet:
>>> 
>>> function longpoll(last_seq) {
>>> var query = $.param({
>>>       since: last_seq || "now",
>>>       include_docs: true,
>>>       feed: 'longpoll',
>>>       heartbeat: 3000
>>>     });
>>> 
>>>  console.log('query ' + query);
>>>     var promise = $.get('/qa_multi_1/_changes?' + query);
>>>     promise.then(function (resp) {
>>>        var parsedResp = JSON.parse(resp),
>>>            results = parsedResp.results,
>>>            last_seq = parsedResp.last_seq;
>>> 
>>>        console.log('changes', resp, parsedResp);
>>> 
>>>        longpoll(last_seq);
>>>      });
>>> }
>>> longpoll("now");
>>> 
>>> 
>>> Any help greatly appreciated
>>> Garren
>> 


Mime
View raw message