Skip to content

Instantly share code, notes, and snippets.

@usingthesystem
Forked from Deele/sse.html
Created July 1, 2023 04:16

Revisions

  1. Nils revised this gist Oct 22, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions stream.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    ob_end_clean();
    //ob_end_clean();
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    //header('Connection: keep-alive');
    @@ -9,8 +9,8 @@
    echo 'id: ' . uniqid() . PHP_EOL;
    echo 'data: ' . date("h:i:s", time()) . PHP_EOL;
    echo PHP_EOL;
    ob_flush();
    flush();
    //ob_flush();
    //flush();

    sleep(1);
    }
  2. Nils revised this gist Oct 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stream.php
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@
    //header('Connection: keep-alive');
    while (true) {


    //echo "retry: 1000" . PHP_EOL;
    echo 'id: ' . uniqid() . PHP_EOL;
    echo 'data: ' . date("h:i:s", time()) . PHP_EOL;
    echo PHP_EOL;
    ob_flush();
  3. Nils revised this gist Oct 22, 2015. 2 changed files with 4 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion test.html → sse.html
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    <script>
    if (typeof(EventSource) !== 'undefined') {
    console.info('Starting connection...');
    var source = new EventSource('/test.php');
    var source = new EventSource('/stream.php');
    source.addEventListener('open', function(e) {
    console.info('Connection was opened.');
    }, false);
    6 changes: 3 additions & 3 deletions test.php → stream.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    <?php
    ob_end_clean();
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    //header('Connection: keep-alive');
    while (true) {
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    //header('Connection: keep-alive');


    //echo "retry: 1000" . PHP_EOL;
  4. Nils revised this gist Oct 22, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion test.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,13 @@
    <?php
    ob_end_clean();
    while (true) {
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    //header('Connection: keep-alive');

    echo ' server time: ' . date("h:i:s", time()) . PHP_EOL;

    //echo "retry: 1000" . PHP_EOL;
    echo 'data: ' . date("h:i:s", time()) . PHP_EOL;
    echo PHP_EOL;
    ob_flush();
    flush();
  5. Nils revised this gist Oct 22, 2015. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions test.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    <?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    header('Connection: keep-alive');

    while (true) {
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');

    echo ' server time: ' . date("h:i:s", time()) . PHP_EOL;
    echo PHP_EOL;
  6. Nils created this gist Oct 22, 2015.
    37 changes: 37 additions & 0 deletions test.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <html>
    <body>
    <div id="result"></div>
    <script>
    if (typeof(EventSource) !== 'undefined') {
    console.info('Starting connection...');
    var source = new EventSource('/test.php');
    source.addEventListener('open', function(e) {
    console.info('Connection was opened.');
    }, false);

    source.addEventListener('error', function(e) {
    var txt;
    switch (event.target.readyState) {
    // if reconnecting
    case EventSource.CONNECTING:
    txt = 'Reconnecting...';
    break;
    // if error was fatal
    case EventSource.CLOSED:
    txt = 'Connection failed. Will not retry.';
    break;
    }
    console.error('Connection error: ' + txt);
    }, false);

    source.addEventListener('message', function(e) {
    document.getElementById('result').innerHTML += e.data + '<br>';
    console.log(e.data);
    }, false);
    } else {
    alert('Your browser does not support Server-sent events! Please upgrade it!');
    console.error('Connection aborted');
    }
    </script>
    </body>
    </html>
    14 changes: 14 additions & 0 deletions test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    header('Connection: keep-alive');

    while (true) {

    echo ' server time: ' . date("h:i:s", time()) . PHP_EOL;
    echo PHP_EOL;
    ob_flush();
    flush();

    sleep(1);
    }