Timmmm
Timmmm

Reputation: 97138

EventSource messages not received in Firefox

I have a server that sends a test EventSource message like this:

Request:

GET /web/stream/status HTTP/1.1
Host: localhost:1010
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: text/event-stream
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:1010/web/
Cookie: JSESSIONID=1miz08s4nu74q11sm7y44uwu2b
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Response:

HTTP/1.1 200 OK
Content-Type: text/event-stream;charset=UTF-8
Connection: close
Server: Jetty(9.0.6.v20130930)

event: data
data: hello

All lines are terminated with \r\n. So that looks right to me but if I try this in Firefox...

var source = new EventSource('/web/stream/status');
source.onmessage = function(event) { console.log(event); };
source.onerror = function(event) { console.log(event); };

... then it connects and performs the request exactly as above (in fact I copied the session from Wireshark into telnet to test it), and according to Wireshark the event: data stuff is sent, but neither the onmessage nor onerror handlers are called. onerror is called when I stop the server.

No data is ever shown in the Response tab of the network thingy.

Does anyone have any ideas what is wrong?

Upvotes: 1

Views: 1706

Answers (1)

Timmmm
Timmmm

Reputation: 97138

Aha I found the answer! Firefox does not like the ;charset=UTF-8. The spec says that you can have ;charset=utf-8 which Firefox allows.

Rather strict interpretation of the spec but fair enough.

Furthermore, you only get onmessage() for lone data: lines. If the data: is preceeded by and event: name then onmessage() is not called - instead you have to use this:

source.addEventListener('name_of_my_event', myEventHandler, false);

Upvotes: 3

Related Questions