I am trying to pass parameters to a PHP script that is used to stream server-side events. I am using Javascript to listen to the events. I have tried to pass parameters by appending a query string to the server-side script url:
Javascript:
var source = new EventSource("demo_sse.php?file=query");
demo_sse.php:
<?php
header("Content-Type: text/event-stream\n\n");
echo 'data:'.$_GET["file"]."\n\n";
?>
I would expect to see "query" displayed, but instead I get no output.
I also tried:
echo 'data:'.$_SERVER['QUERY_STRING']."\n\n";
But still no output. I have verified that the script works otherwise (can print explicit strings).
I haven't found much on the subject, but I did find a couple of examples of appending the query string to the url in the EventSource argument: Server Sent Events Stop Start with new parameter.
I have tried the following code and SSE seemed to work:
var es = new EventSource('http://localhost/ssed.php?asdf=test111');
es.onmessage=function(e){console.log(e.data);}
ssed.php:
<?php
header("Content-Type: text/event-stream\n\n");
echo 'data:'.$_GET['asdf']."\n\n";
How is your onmessage function written? Is it possible that your browser doesn't support SSE?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With