News for hackers. Stuff that matters.
Re:Back to basics: Why we chose long-polling over websockets by mike-cardwell on Sunday January 05, 2025 @04:09PM (#42605065)
That race condition has nothing to do with long polling, it's just poor design. The sender should stick the message in a queue and the client reads from that queue. Perhaps with the client specifying the last id it saw.
Re:Back to basics: Why we chose long-polling over websockets by lmm on Sunday January 05, 2025 @07:20PM (#42606262)
> That race condition has nothing to do with long polling, it's just poor design. The sender should stick the message in a queue and the client reads from that queue.

How does that help? You can't pop from a queue over HTTP because when the client disconnects you don't know whether it saw your response or not.

Re:Back to basics: Why we chose long-polling over websockets by profmonocle on Sunday January 05, 2025 @07:29PM (#42606321)
The next long-polling request can include a list of the ID(s) returned in the previous request. You keep the messages in the queue until you get the next request ack'ing them.
Re:Back to basics: Why we chose long-polling over websockets by thayne on Sunday January 05, 2025 @10:02PM (#42607197)
That means you have to keep them in the queue until the next time the client connects, which could be a very long time
Re:Back to basics: Why we chose long-polling over websockets by jlokier on Sunday January 05, 2025 @11:11PM (#42607586)
To get reliable message delivery, you have to do that when using WebSockets or SSE too, because those also disconnect or time out depending on upstream network conditions outside the client's control, and will lose messages during reconnection if you don't have a sender-side retransmt queue.

However, queued messages don't have to be kept for a very long time, usually. Because every connection method suffers from this problem, you wouldn't usually architect a system with no resync or reset strategy in the client when reconnection takes so long that it isn't useful to stream every individual message since the last connection.

The client and/or server have a resync timeout, and the server's queue is limited to that timeout, plus margin for various delays.

Once there's a resync strategy implemented, it is often reasonable for rhe server to be able to force a resync early, so it can flush messages queues according to other criteria than a strict timeout. For example memory pressure or server restarts.

Re:Back to basics: Why we chose long-polling over websockets by thayne on Monday January 06, 2025 @05:41AM (#42609427)
With websockets, the client can immediately acknowledge receipt of a message, since the connection is bidirectional. And on a resync the server can just send events that the client never acknowledged.
Re:Back to basics: Why we chose long-polling over websockets by ghusbands on Monday January 06, 2025 @06:37AM (#42609736)
That (to quote you) means you have to keep them in the queue until the next time the client connects, which could be a very long time.

To be clear, there's no real difference - in both cases you have to keep messages in some queue and potentially resend them until they've been acknowledged.

Re:Back to basics: Why we chose long-polling over websockets by mike-cardwell on Monday January 06, 2025 @10:58AM (#42611627)
As the other guy said, that's why I mentioned using an ID. And as the other guy said, the same as required regardless of what channel you're using.
Re:Back to basics: Why we chose long-polling over websockets by profmonocle on Sunday January 05, 2025 @07:34PM (#42606353)
And it's worth noting that you can't just ignore this problem if you're using websockets - websockets disconnect sometimes for a variety of reasons. It may be less frequent than a long-polling timeout, but if you don't have some mechanism of detecting that messages weren't ack'd and retransmitting them the next time the user connects, messages will get lost eventually.