Fork us on GitHub

TIP: Auto Reconnect Web Socket

Keep your app running smoothly with bad networking
TIP: Auto Reconnect Web Socket

TIP: Auto Reconnect Web Socket

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.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.