[erlang-questions] Server Sent Events

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Wed Aug 23 12:38:08 CEST 2017


Here is what I think is going wrong, though I'm not entirely sure:

The Yaws yaws_sse:data/1,2 functions assume the data you give them are
so-called iodata(). So when you write something like

["Button1", "Button2"]

and send it, it is effectively flattened into a binary

<<"Button1Button2">>

The iodata() system is really nice for doing scatter/gatter (typo intended)
IO, but you have to invent some kind of framing in which you run.

For example, we could use JSON:

X = [<<"Button1">>, <<"Button"2>>],
Data = yaws_sse:data(jsx:encode(X)),
...

which encodes the data into a JSON structure which we can handle on the
client side.

Also, look up the iodata() concept. It is pretty nice. There is an old post
by James Hague on it:

http://prog21.dadgum.com/70.html


On Wed, Aug 23, 2017 at 12:27 PM Gattu shiva krishna <
gattushivakrishna@REDACTED> wrote:

> Hi all,
>
>          I'm learning to use yaws web-server and I came across server
> sent events which is used to send data to the client. To send data to
> the client I'm using the function yaws_sse:send_events(A#arg.clisock,
> Data). What is the format of arguments that need to be given to this
> function. I've googled enough and didn't get any satisfactory results.
> Below is a sample application where I'm trying to experiment on
> server-sent-events.
>
>      Here in code I'm trying to create buttons dynamically in client
> side based on data that comes from server side.
>
>
> ---------------------------------------------------------------------------------------------------------------------------------------
>
> SERVER SIDE CODE:
>
>   <erl>
>          out(A) ->
>              Data = yaws_sse:data(["Button1",
> "Button2","Button3","Button4"]),
>
>              yaws_sse:send_events(A#arg.clisock, Data).
>      </erl>
>
>
> ---------------------------------------------------------------------------------------------------------------------------------------
>
> CLIENT SIDE CODE:
>
> <script type="text/javascript">
>        var source = new EventSource("button.yaws");
>        source.onmessage = function(event) {
>              var i, buttonsToCreate,buttonContainer, newButton;
>
>          buttonsToCreate = event.data;
>              buttonContainer =
> document.getElementById('this_element_contains_my_buttons');
>          for (i = 0; i <= buttonsToCreate.length ; i++)
>          {
>                newButton = document.createElement('input');
>                newButton.type = 'button';
>                newButton.value = buttonsToCreate[i];
>                newButton.id = buttonsToCreate[i];
>                buttonContainer.appendChild(newButton);
>          }
>      };
>
>      </script>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170823/60267b72/attachment.htm>


More information about the erlang-questions mailing list