Weird EventSource problems? Watch your content-length!

Super short version: If you're using Server Sent Events, and it doesn't work in Firefox because "Firefox can't establish a connection to the server at..." and it's not a CORS problem... make sure you're not setting a content-length header. The end.

Longer version: A (very cool) project I'm working on at the moment uses Server Sent Events to push real-time monitoring data. If the EventSource object is not found in the browser, or there's a problem connecting, then it will fall back to comet.

Firefox was falling back to comet - even though it happily supports Server Sent Events. The only information I could find about this was "it's a cross domain issue". Which lead me on a long journey of blaming my reverse proxy, and other obviously incorrect theories.

After implementing a "hello, world" version which worked - and doing some header inspections showed that the server was adding a "CONTENT-LENGTH: -1" header - which Firefox certainly doesn't like. Removing or setting a positive value for the length, and all was happy. Though removing it is of course the correct answer: if you set a value for it then the connection is closed after the content length value is reached, and the browser re-connects.

The end.