Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions share/doc/src/api/database/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@ parameter.

}

If you set a heartbeat interval (using the ``heartbeat`` query argument), CouchDB will
send a ``hearbeat`` event that you can subscribe to with:

.. code-block:: javascript

source.addEventListener('heartbeat', function () {}, false);

This can be monitored by the client application to restart the EventSource connection if
needed (i.e. if the TCP connection gets stuck in a half-open state).

.. note::

EventSource connections are subject to cross-origin resource sharing
Expand Down
21 changes: 20 additions & 1 deletion share/www/script/test/changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ couchTests.changes = function(debug) {

xhr = CouchDB.newXhr();

//verify the hearbeat newlines are sent
//verify the heartbeat newlines are sent
xhr.open("GET", CouchDB.proxyUrl("/test_suite_db/_changes?feed=continuous&heartbeat=10&timeout=500"), true);
xhr.send("");

Expand Down Expand Up @@ -171,6 +171,25 @@ couchTests.changes = function(debug) {
T(results[1].changes[0].rev == docBar._rev);
}

// test that we receive EventSource heartbeat events
if (!!window.EventSource) {
var source = new EventSource(
"/test_suite_db/_changes?feed=eventsource&heartbeat=10");

var count_heartbeats = 0;
source.addEventListener('heartbeat', function () { count_heartbeats = count_heartbeats + 1; } , false);

waitForSuccess(function() {
if (count_heartbeats < 3) {
throw "keep waiting";
}
return true;
}, "eventsource-heartbeat");

T(count_heartbeats >= 3);
source.close();
}

// test longpolling
xhr = CouchDB.newXhr();

Expand Down
2 changes: 2 additions & 0 deletions src/couchdb/couch_httpd_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ handle_changes_req2(Req, Db) ->
io_lib:format("\n],\n\"last_seq\":~w}\n", [EndSeq])
),
end_json_response(Resp);
(timeout, "eventsource") ->
send_chunk(Resp, "event: heartbeat\ndata: \n\n");
(timeout, _) ->
send_chunk(Resp, "\n")
end
Expand Down