Websocket timing: How to get the exact time when a websocket message arrives to the browser?
Asked Answered
S

2

6

I'm writing a web application that use websockets for bidirectional communication between the client and the server. My main concern is user-perceived latency, so, I am measuring and profiling whatever I can. In particular, I'm capturing the current time at the onmessage() event. This is useful, but I also want to know when the event has been pushed into the browser's event loop - which happens before the onmessage event is fired.

In Chrome Developer Tools, I see the times in the "Network->Frames" tab, which, I think, is the time when the event enters the event loop. But I need to capture this programmatically in Javascript. Any idea how to do this?

I did some "console.log"ing and saw in a few cases a difference of approximately 10 milliseconds between the time showing in Developer Tools, and the time I capture in the onmessage event. I want my measurements to show if the difference is always as small as 10 milliseconds, or whether sometimes the difference is much higher, due to rendering or some other thing that happens in the page.

enter image description here

Sholeen answered 13/1, 2016 at 7:22 Comment(2)
Are you actually experiencing an issue with use perceived latency or just suffering from premature optimization? If you are having an issue, are you actually sending 30k pings every second? That kind of pinging could bring a site like healthcare.gov down... Finally, most humans aren't going to perceive a difference in 10ms, so if you're able to capture a time in code that 10ms off of what you're seeing in in dev. tools, that's good enough, move on the the next problem.Wolfgram
Latency is my major concern, it is actually occurring, and I al looking for ways to reduce it, so I am studying the influence of different parameters on the latency, such as message size, physical locations, number of concurrent users, etc... The reason I al posting this question is because I am not sure that my measurements are only 10 milliseconds off. The browser seems to know when a message arrived, so I am thinking that there may be a way to capture this information. And regarding the 30k, this is just one of my many experiments. It is not necessarily a typical message size.Sholeen
P
1

The browser api for websocket is too restricted to expose the information that you want.

Browsers started to expose timing informations with the Performance interface, but that interface will only tell to you the timing informations of the initial connection to the websocket server, it don't know about websocket frames

Pummel answered 22/1, 2016 at 13:23 Comment(1)
Ok. Thanks for sharing.Sholeen
D
0

Based on your description of the problem, it is not necessary. The delay between when the http stack receives your message and when it passes it to you in the application so it can be programmatically logged is negligible and almost certainly below the precision of the javascript datetime value (you could use performance.now, but I have my doubts as to how precise it actually is).

Your latency is going to be driven by network factors and server response time - if you can get reasonable measurements of those, you will be where you want to be. The other factors may contribute to measurement "noise" - so long as it is less than 10% of the value you are trying to measure, there are no issues.

Dactylic answered 2/2, 2016 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.