Skip to content Skip to sidebar Skip to footer

Displaying Of Only 10 Recent Server Sent Event Messages All The Time

On this page, I was able to alter their script to run in reverse, showing the lastest event at the top of the list: <

Solution 1:

You can paste this in their try it window to see it working.

<!DOCTYPE html>
<html>
    <body>
        <h1>Getting server updates</h1>
        <div id="result"></div>

        <script>
            messages = new Array();

            if(typeof(EventSource) !== "undefined") {
                var source = new EventSource("demo_sse.php");
                source.onmessage = function(event) {
                    if (messages.length == 10) messages.splice(-1, 1);
                    messages.splice(0, 0, event.data);
                    document.getElementById("result").innerHTML = messages.join("<br />");
                };
            } else {
                document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
            }
        </script>
    </body>
</html>

Post a Comment for "Displaying Of Only 10 Recent Server Sent Event Messages All The Time"