[erlang-questions] Server Sent Events

Steve Vinoski vinoski@REDACTED
Thu Aug 24 15:10:19 CEST 2017


On Thu, Aug 24, 2017 at 1:53 AM, Gattu shiva krishna <
gattushivakrishna@REDACTED> wrote:

> Hi,
>
>         when i'm using below function
>
>         Data = yaws_sse:data( [ "button1", "button2" ] ),
>
>         the result value of that function is
>
>         [<<"data:">>, [ "button1", "button2" ], <<"\n">>]
>

As Jesper already explained, what the client will receive in this case is
the equivalent of

<<"data:button1button2\n">>

Is that really what you're trying to send? Parsing that seems difficult.


>         when i'm trying to send this data to client side using below
> function
>         yaws_sse:send_events(A#arg.clisock, Data)
>
>         but in client side iam unable to retrieve the data, Is there any
> solution for this.
>
> ------------------------------------------------------------
> ------------------------------------------------------------
> ----------------------
>
>         when i'm trying to use this  function ( yaws_sse:send_events/2
> )manually in yaws shell.
>
>         for example :
>
>         1> yaws_sse:send_events( '#port<0.3333>', [<<"data:">>,[
> "button1", "button2" ],<<"\n">>]).
>
>         It is giving some error like this :
>
>         ** exception error: no function clause matching
>                     gen_tcp:send('#port<0.3333>',
>                                  [[<<"data:">>,
>                                    [ "button1", "button2" ],
>                                    <<"\n">>],
>                                   <<"\n">>]) (gen_tcp.erl, line 261)
>
>     What is the format of arguments that need to be given to this function.
>

You're getting this error because you're passing the port argument as an
atom, not as a port. Assuming you're using a relatively recent version of
Erlang/OTP, in the shell the port you've mentioned should be specified as

#Port<0.3333>

without quotes. The whole call becomes

yaws_sse:send_events(#Port<0.3333>, [<<"data:">>,[ "button1", "button2"
],<<"\n">>]).

or more simply but equivalently

yaws_sse:send_events(#Port<0.3333>, <<"data:button1button2\n">>).

which will send the data you specify, assuming #Port<0.3333> is really the
right connection. But again, the data you're sending is poorly structured
and so won't be easy for your client to deal with.

--steve


> On Wednesday 23 August 2017 04:08 PM, Jesper Louis Andersen wrote:
>
> 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
>>
>
>
> _______________________________________________
> 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/20170824/c231f7f6/attachment.htm>


More information about the erlang-questions mailing list