aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers/sse_clock_h.erl
blob: 23b834fd3c1890cda23d9ae33f92478ddf38c296 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
%% This module implements a loop handler that sends
%% the current time every second using SSE.

-module(sse_clock_h).

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

init(Req, State) ->
	self() ! timeout,
	{cowboy_loop, cowboy_req:stream_reply(200, #{
		<<"content-type">> => <<"text/event-stream">>
	}, Req), State}.

info(timeout, Req, State) ->
	erlang:send_after(1000, self(), timeout),
	Time = calendar:system_time_to_rfc3339(erlang:system_time(second)),
	cowboy_req:stream_events(#{
		data => Time
	}, nofin, Req),
	{ok, Req, State}.