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


                                                           
                       
 
                  
                  
 

                                             
                                                             
                 
                                                           
                                 

                                   



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

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

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

init(Req0, Opts) ->
	Req = cowboy_req:stream_reply(200, #{
		<<"content-type">> => <<"text/event-stream">>
	}, Req0),
	erlang:send_after(1000, self(), {message, "Tick"}),
	{cowboy_loop, Req, Opts}.

info({message, Msg}, Req, State) ->
	cowboy_req:stream_events(#{
		id => id(),
		data => Msg
	}, nofin, Req),
	erlang:send_after(1000, self(), {message, "Tick"}),
	{ok, Req, State}.

id() ->
	integer_to_list(erlang:unique_integer([positive, monotonic]), 16).