I created a real-time server using

I created a real-time server using Durable Objects and WebSocket. The logs confirm that both the Worker and the DO are in the SJC data center. Pinging the Worker's domain shows a latency of 2ms, but when messages are returned via WebSocket, the latency is as high as 70ms. Is the communication latency between the Worker and the DO really that high?
42 Replies
Milan
Milan2w ago
When you say pinging the domain, do you literally mean ping <the domain>, or do you mean doing an HTTP request to some /ping endpoint? Those are very different things
geekergame
geekergame2w ago
ping <the domain> The HTTPS GET request to the worker takes 18ms.
Milan
Milan2w ago
And how long to the DO? ^ Note that for request to the DO, you don't want the first request cuz that'll cold start and DOs are heavier than Workers
geekergame
geekergame2w ago
The DO will send one packet to the client every second, containing the time obtained from performance.now(). Upon receiving the packet, the client will immediately send it back. The DO will then subtract the time in the packet from the current time obtained using performance.now() to calculate the round-trip latency. The observed latency is currently between 60-90ms. I hope that users near the data center will have WebSocket latency below 50ms, as latency above 50ms results in a 3-frame input delay. If there are other solutions, I am open to trying them. Thank you for your support.
Milan
Milan7d ago
How are you advancing time exactly? WS send/receive are not async at JS level. Are you creating a promise (when your DO sends) that resolves when your DO receives, and then timing the duration of that promise resolving? Also what's one "packet" in this case?
geekergame
geekergame7d ago
let lagBuffer = new Uint16Array(5); lagBuffer[0] = performance.now()&0xFFFF; packet like this this is onmessage handler let lagInfo = new Uint16Array(data); this.PLag[pos] = (performance.now()&0xFFFF) - lagInfo[0]; ws send is sync?
Milan
Milan7d ago
Also, is your DO handling any other requests at that time? Keep in mind they're single threaded, so if another request is running them wall time will definitely be higher
geekergame
geekergame7d ago
how to change async
Milan
Milan7d ago
ws.send() in javascript is a synchronous method because you "receive" the response in your response handler Well, let me rephrase, there's nothing to await when you call ws.send(). It just returns void It queues a message to be sent asynchronously Could you format this? It's kind of hard to read, you can use backticks `
Milan
Milan7d ago
The reason I bring up sync/async is because of https://developers.cloudflare.com/workers/runtime-apis/performance/#performancenow (specifically for perf timing)
Cloudflare Docs
Performance and timers | Cloudflare Workers docs
Measure timing, performance, and timing of subrequests and other operations.
geekergame
geekergame7d ago
No description
No description
geekergame
geekergame7d ago
No description
geekergame
geekergame7d ago
thanks,i will to try After trying, it seems there is no significant difference. Is there any way to achieve the lowest latency
Milan
Milan7d ago
What is your expected latency exactly? I think you need to test the latency of invoking a DOs fetch() handler (not the Worker in front of it) to get a better sense for latency to your DO. Also, when you test this, are you sending any other requests to the DO? It's important to keep in mind DO's are single threaded so if any other requests are being processed, we won't deliver the clients response to the DO until it is its turn to execute.
geekergame
geekergame7d ago
A single fetch operation returns an object immediately within 5-12 milliseconds, which is very ideal. DO only handles lag tests continuously, with a single websocket and no other requests. I want to develop a real-time websocket server that only has workers and DO, so there is no other backend.
Want results from more Discord servers?
Add your server