<html>
  <head>
    <style type="text/css">
      #changes div {
        border-bottom: 1px solid gray;
        margin-bottom: 1em;
      }
    </style>
  </head>
  <body>
    <div id="body">
      <div id="changes">
        <h4>Changes</h4>
        {% for change in changes %}
          {% raw change['html'] %}
        {% end %}
      </div>
    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
$(document).ready(function() {
    updater.start();
});

var updater = {
    socket: null,

    start: function() {
        var url = "ws://" + location.host + "/socket";
        updater.socket = new WebSocket(url);
        updater.socket.onmessage = function(event) {
            updater.showMessage(JSON.parse(event.data));
        }
    },

    showMessage: function(change) {
        var container = $("#changes");
        var existing = $("#change-" + change.id);
        if (existing.length > 0) return;

        while (container.find('div').length >= 5) {
          container.find('div:first').remove();
        }

        var node = $(change.html);
        container.append(node);
    }
};
    </script>
  </body>
</html>
