[erlang-questions] Server Sent Events

Gattu shiva krishna gattushivakrishna@REDACTED
Wed Aug 23 12:27:33 CEST 2017


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>




More information about the erlang-questions mailing list