Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>here is a simple Server Sent Event Script using php.</p> <p>support </p> <p><a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/API/EventSource</a></p> <p>js</p> <pre><code>var sse=new EventSource("sse.php"); sse.onmessage=function(e){ document.body.innerHTML=e.data; }; </code></pre> <p>sse.php</p> <pre><code>header('Content-Type: text/event-stream'); // specific sse mimetype header('Cache-Control: no-cache'); // no cache while(true) { if(/*something changes*/){ echo "id: ".time().PHP_EOL; echo "data: ".$data.PHP_EOL; echo PHP_EOL; } ob_flush(); // clear memory flush(); // clear memory sleep(10);// seconds } </code></pre> <p>this keeps the connection open with the client, </p> <p>then it checks if something is changed ... db/file whatever</p> <p>outputs the data if changed </p> <p>and then clears the php cache </p> <p>waits 10 seconds and does it again.</p> <hr> <p>As you can see the client recieves the data only if something changes on the server</p> <p>but i totally don't know how the server could handle 1000's of people.</p> <hr> <p>node.js would be a better way. but it depends what languages you are using and/or if you can actually use node.js.</p> <p>websockets is both ways.</p> <p>server sent event is oneway.(you need this)</p> <p><strong>EDIT</strong></p> <p>more data inside the sse response:</p> <p>js</p> <pre><code>var sse=new EventSource("sse.php"); sse.onmessage=function(e){ console.log(JSON.parse(e.data)) }; </code></pre> <p>sse.php</p> <pre><code>header('Content-Type: text/event-stream'); // specific sse mimetype header('Cache-Control: no-cache'); // no cache while(true) { if(/*something changes*/){ echo "id: ".time().PHP_EOL; $dataArray=array('id'=&gt;$id,'name'=&gt;$name,'more'=&gt;$more); echo "data: ".json_encode($dataArray).PHP_EOL; echo PHP_EOL; } ob_flush(); // clear memory flush(); // clear memory sleep(10);// seconds } </code></pre> <p>if you need some more info just ask</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload