You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2014/01/18 00:07:51 UTC
[04/12] git commit: add eventsource support
add eventsource support
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/commit/936b7c34
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/tree/936b7c34
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/diff/936b7c34
Branch: refs/heads/import
Commit: 936b7c348e762d9fbc80543bb49dab39ca415172
Parents: c1c1cd0
Author: benoitc <bc...@gmail.com>
Authored: Thu May 10 23:09:43 2012 +0200
Committer: benoitc <bc...@gmail.com>
Committed: Thu May 10 23:09:43 2012 +0200
----------------------------------------------------------------------
README.md | 3 ++-
src/couch_dbupdates_httpd.erl | 19 +++++++++++++++++--
2 files changed, 19 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/936b7c34/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 6d631e5..fc068e6 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,8 @@ It's actually **supported by all the [refuge](http://refuge.io) projects**:
To get db events, do a GET to `/_db_updates` .
You can pass an optional query parameter `feed`. The feed can be
-`longpoll` (default) or `continuous`.
+`longpoll` (default) for longpolling, `eventsource` for event stream or
+`continuous` for continuous feed.
## Example of usage
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/936b7c34/src/couch_dbupdates_httpd.erl
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates_httpd.erl b/src/couch_dbupdates_httpd.erl
index 671f77c..d572dc6 100644
--- a/src/couch_dbupdates_httpd.erl
+++ b/src/couch_dbupdates_httpd.erl
@@ -8,10 +8,19 @@
-record(state, {resp, feed}).
handle_req(#httpd{method='GET'}=Req) ->
- {ok, Resp} = couch_httpd:start_json_response(Req, 200),
-
Feed = proplists:get_value("feed", couch_httpd:qs(Req), "longpoll"),
+ {ok, Resp} = case Feed of
+ "eventsource" ->
+ Headers = [
+ {"Content-Type", "text/event-stream"},
+ {"Cache-Control", "no-cache"}
+ ],
+ couch_httpd:start_json_response(Req, 200, Headers);
+ _ ->
+ couch_httpd:start_json_response(Req, 200)
+ end,
+
State = #state{resp=Resp, feed=Feed},
couch_dbupdates:handle_dbupdates(fun handle_update/2,
State).
@@ -22,6 +31,12 @@ handle_req(Req, _Db) ->
handle_update(stop, #state{resp=Resp}) ->
couch_httpd:end_json_response(Resp);
+handle_update(Event, #state{resp=Resp, feed="eventsource"}=State) ->
+ EventObj = event_obj(Event),
+ {ok, Resp1} = couch_httpd:send_chunk(Resp, ["data: ",
+ ?JSON_ENCODE(EventObj),
+ "\n"]),
+ {ok, State#state{resp=Resp1}};
handle_update(Event, #state{resp=Resp, feed="continuous"}=State) ->
EventObj = event_obj(Event),
{ok, Resp1} = couch_httpd:send_chunk(Resp, [?JSON_ENCODE(EventObj) |