forked from cybergrind/rebar-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cowboy_eventsource.erl
38 lines (33 loc) · 1.34 KB
/
cowboy_eventsource.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%%%-------------------------------------------------------------------
%%% @author {{author_name}} <{{author_email}}>
%%% @copyright (C) {{copyright_year}}, {{copyright_owner}}
%%% @doc
%%%
%%% @end
%%% Created : {{now_ts}} by {{author_name}} <{{author_email}}>
%%%-------------------------------------------------------------------
-module('{{handlerid}}_eventsource').
-export([init/3]).
-export([info/3]).
-export([terminate/3]).
%%%===================================================================
%%% Cowboy callbacks
%%%===================================================================
init(_Transport, Req, []) ->
Headers = [{<<"content-type">>, <<"text/event-stream">>}],
{ok, Req2} = cowboy_req:chunked_reply(200, Headers, Req),
erlang:send_after(1000, self(), {message, "Tick"}),
{loop, Req2, undefined, 5000}.
info({message, Msg}, Req, State) ->
ok = cowboy_req:chunk(["id: ", id(), "\ndata: ", Msg, "\n\n"], Req),
erlang:send_after(1000, self(), {message, "Tick"}),
{loop, Req, State}.
terminate(_Reason, _Req, _State) ->
ok.
%%%===================================================================
%%% Internal functions
%%%===================================================================
id() ->
{Mega, Sec, Micro} = erlang:now(),
Id = (Mega * 1000000 + Sec) * 1000000 + Micro,
integer_to_list(Id, 16).