Open Source & Free  

TIP: Auto Reconnect Web Socket

TIP: Auto Reconnect Web Socket

Header Image

WebSockets changed the way I do networking code. I combine them with WebServices to get the best of both worlds. But they still suffer in terms of reliability. With WebServices we have retries and a mostly transactional model. There is no permanent connection that should be re-established.

With WebSockets a disconnect can be painful, up until recently I used a rather elaborate strategy of error detection and timers. With the latest update to the WebSocket cn1lib we now have a better solution: autoReconnect(int).

It’s exactly as it sounds, once you create a websocket you can invoke autoReconnect(5000) on it to retry the connection ever 5 seconds in case of a disconnect.

If you have an existing WebSocket app you should probably add this call.

Leave a Reply