couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chewbra...@apache.org
Subject chttpd commit: updated refs/heads/1843-feature-bigcouch-clustered-eventsource-changes-feed to 6a74f9b
Date Thu, 22 May 2014 00:22:42 GMT
Repository: couchdb-chttpd
Updated Branches:
  refs/heads/1843-feature-bigcouch-clustered-eventsource-changes-feed [created] 6a74f9b99


WIP: working eventing source changes feeds


Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/6a74f9b9
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/6a74f9b9
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/6a74f9b9

Branch: refs/heads/1843-feature-bigcouch-clustered-eventsource-changes-feed
Commit: 6a74f9b9903b6b1291eb8cab29423ea6cb68303b
Parents: 644ca97
Author: Russell Branca <chewbranca@apache.org>
Authored: Wed May 21 17:22:39 2014 -0700
Committer: Russell Branca <chewbranca@apache.org>
Committed: Wed May 21 17:22:39 2014 -0700

----------------------------------------------------------------------
 src/chttpd_db.erl | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/6a74f9b9/src/chttpd_db.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_db.erl b/src/chttpd_db.erl
index 0c08dd9..db7c9bb 100644
--- a/src/chttpd_db.erl
+++ b/src/chttpd_db.erl
@@ -72,7 +72,7 @@ handle_changes_req(#httpd{method='GET'}=Req, Db) ->
                 ChangesArgs)
         end);
     Feed ->
-        % "longpoll" or "continuous"
+        % "longpoll" or "continuous" or "eventsource"
         fabric:changes(Db, fun changes_callback/2, {Feed, Req}, ChangesArgs)
     end;
 handle_changes_req(#httpd{path_parts=[_,<<"_changes">>]}=Req, _Db) ->
@@ -91,6 +91,26 @@ changes_callback({stop, EndSeq0}, {"continuous", Resp}) ->
         [?JSON_ENCODE({[{<<"last_seq">>, EndSeq}]}) | "\n"]),
     chttpd:end_delayed_json_response(Resp1);
 
+% callbacks for eventsource feed (newline-delimited eventsource Objects)
+changes_callback(start, {"eventsource", Req}) ->
+    Headers = [
+        {"Content-Type", "text/event-stream"},
+        {"Cache-Control", "no-cache"}
+    ],
+    {ok, Resp} = chttpd:start_delayed_json_response(Req, 200, Headers),
+    {ok, {"eventsource", Resp}};
+changes_callback({change, {ChangeProp}=Change}, {"eventsource", Resp}) ->
+    Seq = proplists:get_value(seq, ChangeProp),
+    Chunk = [
+        "data: ", ?JSON_ENCODE(Change),
+        "\n", "id: ", ?JSON_ENCODE(Seq),
+        "\n\n"
+    ],
+    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, [Chunk]),
+    {ok, {"eventsource", Resp1}};
+changes_callback({stop, _EndSeq}, {"eventsource", Resp}) ->
+    chttpd:end_delayed_json_response(Resp);
+
 % callbacks for longpoll and normal (single JSON Object)
 changes_callback(start, {"normal", {"Etag", Etag}, Req}) ->
     FirstChunk = "{\"results\":[\n",


Mime
View raw message