[erlang-questions] Server Sent Events
Gattu shiva krishna
gattushivakrishna@REDACTED
Thu Aug 24 07:53:41 CEST 2017
Hi,
when i'm using below function
Data = yaws_sse:data( [ "button1", "button2" ] ),
the result value of that function is
[<<"data:">>, [ "button1", "button2" ], <<"\n">>]
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.
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 <mailto: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 <mailto: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/7e91894b/attachment.htm>
More information about the erlang-questions
mailing list