add missing change_callback implementation 2665-db-updates-eventsource 14/head
authorJan Lehnardt <jan@apache.org>
Sun, 17 Apr 2016 18:21:37 +0000 (20:21 +0200)
committerJan Lehnardt <jan@apache.org>
Sun, 17 Apr 2016 18:21:37 +0000 (20:21 +0200)
src/global_changes_httpd.erl

index 07a7353fee35945b5af93bdfd1d95f58cc96cf25..b34b093294653deda31c5f8a981d1695b8c6818b 100644 (file)
@@ -127,6 +127,34 @@ changes_callback({stop, EndSeq, _Pending}, #acc{feed="continuous"}=Acc) ->
         [?JSON_ENCODE({[{<<"last_seq">>, EndSeq}]}) | "\n"]),
     chttpd:end_delayed_json_response(Resp1);
 
+% callbacks for eventsource feed (newline-delimited eventsource Objects)
+changes_callback(start, #acc{feed = "eventsource"} = Acc) ->
+    #acc{resp = Req} = Acc,
+    Headers = [
+        {"Content-Type", "text/event-stream"},
+        {"Cache-Control", "no-cache"}
+    ],
+    {ok, Resp} = chttpd:start_delayed_json_response(Req, 200, Headers),
+    {ok, Acc#acc{resp = Resp, last_data_sent_time=os:timestamp()}};
+changes_callback({change, {ChangeProp}=Change}, #acc{resp = Resp, feed = "eventsource"} = Acc) ->
+    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),
+    maybe_finish(Acc#acc{resp = Resp1});
+changes_callback(timeout, #acc{feed = "eventsource"} = Acc) ->
+    #acc{resp = Resp} = Acc,
+    Chunk = "event: heartbeat\ndata: \n\n",
+    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, Chunk),
+    {ok, {"eventsource", Resp1}};
+changes_callback({stop, _EndSeq}, #acc{feed = "eventsource"} = Acc) ->
+    #acc{resp = Resp} = Acc,
+    % {ok, Resp1} = chttpd:send_delayed_chunk(Resp, Buf),
+    chttpd:end_delayed_json_response(Resp);
+
 % callbacks for longpoll and normal (single JSON Object)
 changes_callback(start, #acc{feed="normal", etag=Etag}=Acc)
         when Etag =/= undefined ->