Javascript Starter Template

A simple Javascript starter template application for using kaazing.io

This template is a minimal application to get you started. Just replace the endpoint URL in the new EventSource(…) line with the SSE endpoint you want to use.

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Polyfill for older browsers without native support for the HTML5 EventSource API. -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=EventSource"></script>
  </head>
  <body>
    <div id="payload"></div>
    <script>

      const stream = new EventSource('https://streams.kaazing.net/sportsbook/events');

      // Handler for when the stream is opened (either the first time or after a reconnect).
      stream.addEventListener('open', function(event) {
        console.log('Stream is open');
      }, false);

      // Handler for new messages.
      stream.addEventListener('message', event => {
        document.getElementById('payload').innerText = event.data;
      }, false);

      // Handler for a dropped connection or the server closing the connection.
      stream.addEventListener('error', function(event) {
        switch (event.target.readyState) {
            case EventSource.CONNECTING:
                console.log('Reconnecting...');
                break;
            case EventSource.CLOSED:
                console.log('Connection failed, will not reconnect');
                break;
        }
      }, false);

    </script>
  </body>
</html>