Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-side event - how to pass parameters to server-side script? Append query string?

Tags:

javascript

php

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.

like image 233
KevinHJ Avatar asked Mar 11 '14 14:03

KevinHJ


1 Answers

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?

like image 176
Schien Avatar answered Sep 24 '22 06:09

Schien