I don't begrudge the author, I'm just surprised given the otherwise high quality of the analysis, including him looking at other parts of the source code.
send = (t: string, d: any, o: any = {}, noRetry = false): void => {
const msg: Partial<MsgOut> = { t };
if (d !== undefined) {
if (o.withLag) d.l = Math.round(this.averageLag);
if (o.millis >= 0) d.s = Math.round(o.millis * 0.1).toString(36);
msg.d = d;
}
if (o.ackable) {
msg.d = msg.d || {}; // can't ack message without data
this.ackable.register(t, msg.d); // adds d.a, the ack ID we expect to get back
}
const message = JSON.stringify(msg);
...
Which is calculated from how long the server takes to respond to ping messages that the client sends: private schedulePing = (delay: number): void => {
clearTimeout(this.pingSchedule);
this.pingSchedule = setTimeout(this.pingNow, delay);
};
private pingNow = (): void => {
clearTimeout(this.pingSchedule);
clearTimeout(this.connectSchedule);
const pingData =
this.options.isAuth && this.pongCount % 10 == 2
? JSON.stringify({
t: 'p',
l: Math.round(0.1 * this.averageLag),
})
: 'null';
try {
this.ws!.send(pingData);
this.lastPingTime = performance.now();
} catch (e) {
this.debug(e, true);
}
this.scheduleConnect();
};
private computePingDelay = (): number => this.options.pingDelay + (this.options.idle ? 1000 : 0);
private pong = (): void => {
clearTimeout(this.connectSchedule);
this.schedulePing(this.computePingDelay());
const currentLag = Math.min(performance.now() - this.lastPingTime, 10000);
this.pongCount++;
// Average first 4 pings, then switch to decaying average.
const mix = this.pongCount > 4 ? 0.1 : 1 / this.pongCount;
this.averageLag += mix * (currentLag - this.averageLag);
pubsub.emit('socket.lag', this.averageLag);
this.updateStats(currentLag);
};