EventSource's response has a MIME type
syntheticsh opened this issue · 3 comments
I'm testing out react with addition of clue/php-sse-reactt, but can't make it work properly.
I'm using server like in the example:
require __DIR__ . '/../vendor/autoload.php';
use Clue\React\Sse\BufferedChannel;
use React\Http\Request;
use React\Http\Response;
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$channel = new BufferedChannel();
$http = new React\Http\Server($socket);
$http->on('request', function (Request $request, Response $response) use ($channel) {
echo 'connected' . PHP_EOL;
$headers = $request->getHeaders();
$id = isset($headers['Last-Event-ID']) ? $headers['Last-Event-ID'] : null;
$response->writeHead(200, array('Content-Type' => 'text/event-stream', 'Cache-Control' => 'no-cache'));
$channel->connect($response, $id);
$response->on('close', function () use ($response, $channel) {
echo 'disconnected' . PHP_EOL;
$channel->disconnect($response);
});
});
$loop->addPeriodicTimer(2.0, function() use ($channel) {
$channel->writeMessage('ticking ' . mt_rand(1, 5) . '...');
});
$socket->listen(1337);
echo 'Server now listening on localhost:' . $socket->getPort() . PHP_EOL;
$loop->run();
Than I run it with bash: $ php server.php
When I'm doing script:
var eventSource = new EventSource('server.php');
it gives an error:
EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.
What might be the problem?
Also I used
$ echo -e "GET / HTTP/1.0\r\n\r\n" | nc localhost 1337
and it returned
HTTP/1.1 200 OK
X-Powered-By: React/alpha
Content-Type: text/event-stream
Cache-Control: no-cache
Transfer-Encoding: chunked
When I'm doing script:
var eventSource = new EventSource('server.php');it gives an error:
EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.
What might be the problem?
It looks like you're serving your javascript through a normal webserver (let's assume http://example.com/sse.js) and thus using the above relative URI results in a full URI of http://example.com/server.php, while your SSE-enabled webserver would run on http://example.com:1337/.
Also check out the examples folder, which includes a working example showing how everything can be served through PHP's webserver. As an alternative, either pass a full URI to your EventSource
or use a reverse proxy in front of both servers.
I hope this helps 👍
Yeah it was a mistake to use var eventSource = new EventSource('server.php');
When I used it like var eventSource = new EventSource('http://example.com:1337/');
it worked, also had to add access-control headers while using absolute URI. Changed this
$socket->listen(1337, 'server-ip');
because with '0.0.0.0' it was only accessible through localhost.
Thanks for the help!
Thanks for the confirmation and glad this worked 👍