aboutsummaryrefslogblamecommitdiffstats
path: root/examples/eventsource/src/eventsource_handler.erl
blob: 3aa60e766f6755fbcc04cb32084e2f7abb3deb68 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                                           
                  
                  
 
                  
                                                                  
                                                           
                                                           
                                         

                                   
                                                                       


                                                           



                                                      
%% Feel free to use, reuse and abuse the code in this file.

%% @doc EventSource emitter.
-module(eventsource_handler).

-export([init/2]).
-export([info/3]).

init(Req, Opts) ->
	Headers = [{<<"content-type">>, <<"text/event-stream">>}],
	Req2 = cowboy_req:chunked_reply(200, Headers, Req),
	erlang:send_after(1000, self(), {message, "Tick"}),
	{long_polling, Req2, Opts, 5000}.

info({message, Msg}, Req, State) ->
	cowboy_req:chunk(["id: ", id(), "\ndata: ", Msg, "\n\n"], Req),
	erlang:send_after(1000, self(), {message, "Tick"}),
	{loop, Req, State}.

id() ->
	{Mega, Sec, Micro} = erlang:now(),
	Id = (Mega * 1000000 + Sec) * 1000000 + Micro,
	integer_to_list(Id, 16).