Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 69 lines (61 sloc) 2.244 kb
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
%%%----------------------------------------------------------------------
%%% File : yaws_sse.erl
%%% Author : Steve Vinoski <vinoski@ieee.org>
%%% Purpose : Support for Server-Sent Events
%%% Created : 31 May 2012 by Steve Vinoski <vinoski@ieee.org>
%%%----------------------------------------------------------------------
-module(yaws_sse).
-author('vinoski@ieee.org').

-export([headers/1,
         event/0, event/1,
         data/0, data/1, data/2,
         id/0, id/1,
         retry/0, retry/1,
         comment/1,
         send_events/2, send_events/3
        ]).

headers(StreamPid) ->
    [{status, 200},
     {header, {"Cache-Control", "no-cache"}},
     {header, {connection, "close"}},
     {header, {transfer_encoding, erase}},
     {streamcontent_from_pid, "text/event-stream", StreamPid}].

event() ->
    <<"event\n">>.
event(EventName) ->
    [<<"event:">>, EventName, <<"\n">>].

data() ->
    <<"data\n">>.
data(Data) ->
    [<<"data:">>, Data, <<"\n">>].
%% The version below trims out all embedded newlines. If you send data
%% containing newlines using the version above, your events will be
%% misinterpreted at the client. If the result of trimming includes
%% any empty strings or binaries, they are dropped and not sent.
data(Data0, [trim]) ->
    Bin = iolist_to_binary(Data0),
    Tokens = case catch binary:split(Bin, <<"\n">>, [global, trim]) of
                 {'EXIT', {undef, [{binary,split,__}|_]}} ->
                     %% handle older releases of Erlang
                     Lst = binary_to_list(Bin),
                     [Tok || Tok <- string:tokens(Lst, "\n")];
                 Bins ->
                     [B || B <- Bins, B /= <<>>]
             end,
    [data(Data) || Data <- Tokens].

id() ->
    <<"id\n">>.
id(Id) when is_integer(Id) ->
    [<<"id:">>, integer_to_list(Id), <<"\n">>];
id(Id) ->
    [<<"id:">>, Id, <<"\n">>].

retry() ->
    <<"retry\n">>.
retry(ReconnectionTime) ->
    [<<"retry:">>, integer_to_list(ReconnectionTime), <<"\n">>].

comment(Comment) ->
    [<<":">>, Comment, <<"\n">>].

send_events(Socket, Events) ->
    send_events(Socket, Events, fun yaws_api:stream_process_deliver/2).
send_events(Socket, Events, SendFun) ->
    SendFun(Socket, [Events, <<"\n">>]).
Something went wrong with that request. Please try again.