Code examples for presentation server-sent events
php -S 127.0.0.1:9001
Then open with browser: http://127.0.0.1:9001
Code examples for presentation server-sent events
php -S 127.0.0.1:9001
Then open with browser: http://127.0.0.1:9001
| <!DOCTYPE html> | |
| <html> | |
| <head lang="en"> | |
| <meta charset="UTF-8"> | |
| <title>Server-Sent events</title> | |
| </head> | |
| <body> | |
| <script type="text/javascript"> | |
| var source = new EventSource("/stream.php"); | |
| source.addEventListener('message', function(e) { | |
| document.body.innerHTML += e.data + '<br/>'; | |
| }, false); | |
| </script> | |
| Welcome to<br/> | |
| </body> | |
| </html> |
| <?php | |
| header('Content-type: text/event-stream'); | |
| for ($i = 0; $i < 10; $i++) { | |
| foreach (['VilniusPHP', 'VilniusJs'] as $event) { | |
| print 'data: ' . $event."\n\n"; | |
| ob_flush(); | |
| flush(); | |
| sleep(1); | |
| } | |
| } |