Sebastian Nowak
Sebastian Nowak

Reputation: 5717

Websockets delay under Firefox

I've created a simple WebSockets server which sends a tiny packet every 20 ms, just for testing purposes. Then I wrote a simple browser client in pure WebSocket api, which connects to the server and prints Date.now() every time it receives a packet.

var ws = new WebSocket('ws://localhost:8080');
ws.onmessage = function(){ console.log(Date.now()); };

When I've launched the client in Google Chrome, the delay between the packets was consistent and always between 19 and 21 ms. When I've launched it under Firefox... uhh, delay was jumping between 3 and 114 ms. What's wrong with the Firefox and what's the workaround? Is there some kind of buffering enabled by default?

Tests were done on localhost under Linux, Firefox version is 38.0.5, packet size was 4 bytes, no additional code was running so garbage collector wasn't the issue. I've also launched both browsers simultaneously, results were the same.

Upvotes: 3

Views: 1848

Answers (1)

Sebastian Nowak
Sebastian Nowak

Reputation: 5717

After lots of testing, the result is quite funny - console.log implementation under Firefox is the root of all evil. Calling it frequently results in short browser hangs which makes all the timers and events go out of sync. When debugging timed events under Firefox, it's currently much better to store logs in an array and print it later.

Upvotes: 3

Related Questions