Long Polling is used when WebSockets cannot connect or the network is unstable.
auto pending = store.dequeue(session_id, max);
return pending;async function poll() {
try {
const res = await fetch("/ws/poll?session_id=123");
const arr = await res.json();
for (const msg of arr) handle(msg);
} catch (e) {
console.log("Offline:", e);
}
poll();
}await fetch("/ws/send", {
method: "POST",
body: JSON.stringify({
session_id: sid,
type: "chat.message",
payload: { text: "Hello" }
})
});- Messages stored in SQLite
- Incrementing IDs
- Poll returns only new messages
- Queue expires older entries
- Try WS
- If WS closes -> LP
- When WS reconnects -> stop LP
- If offline -> queue messages
- Replay queue on reconnect