Rajkamal Subramanian
Rajkamal Subramanian

Reputation: 6944

request header not set to text/event-stream for Eventsource calls

I have the following Eventsource.

 var source = new EventSource('/events');
    source.onmessage = function(e) {
      document.body.innerHTML += e.data + '<br>';
    };

The problem is, the Accept request header is automatically set to text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8.

In node.js i'm handling the request with the following condition.

if (req.headers.accept && req.headers.accept != 'text/event-stream') {..}

Because of this im unable to stream data. Please let me know how to set accept request header to text/event-stream while calling EventSource('/events');

Upvotes: 2

Views: 2493

Answers (2)

Licson
Licson

Reputation: 2271

Why don't you just do this:

if(req.url == '/events'){
    res.writeHead(200,{'content-type':'text/event-stream'});
    //Your sse code here...
    sendMsg('some data...');
}

It's more straightforward and you don't need to deal with the request header problem anymore.

Upvotes: 1

4esn0k
4esn0k

Reputation: 10407

you can not do this the browser sets Accept header for you

an EventSource object makes request with "text/event-stream" accept header under Chrome, Firefox and Opera check, that you are handling the right request

Upvotes: 0

Related Questions