Menu

The chat says "connected" and no messages arrive. Keeping WebSockets alive on mobile networks.
📰

The chat says "connected" and no messages arrive. Keeping WebSockets alive on mobile networks.

DEV Community: ios·MinPin Proxy·11 days ago
#BfzsAeQv
#devtoios#devto#dev#reconnect#server
Long article (827 words) — no AI summary yet.
Reading 0:00
15s threshold

The bug: the app shows a green "connected" dot, but new messages stop arriving. The user pulls to refresh, the missed messages appear, and live updates work again for a while. The server logs show the connection closed minutes ago, or never show it closing at all.

A WebSocket is one long-lived TCP connection. Phones are bad at keeping those, and they often don't tell the app when one dies. Here's why, and what a reliable client does about it.

Why the connection dies without anyone noticing

NAT and carrier timeouts. Mobile networks put devices behind NAT gateways that drop idle TCP mappings, sometimes after only a few minutes without traffic. Neither end gets a close frame or a reset. The next message from the server just disappears.

Network switches. When the phone moves from Wi-Fi to cellular, its IP address changes. The old TCP connection can't move with it. Until a write fails or a timeout fires, the client may still think it's connected.

The app goes to the background.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.