# How to automatically reconnect disconnected WebSockets with autoReconnect(int)

DevFeed: [How to automatically reconnect disconnected WebSockets with autoReconnect(int)](<https://devfeed.tech/articles/tip-auto-reconnect-web-socket-19564.md>)

Original publisher: [Read original article](<https://www.codenameone.com/blog/tip-auto-reconnect-web-socket/>)

Author: Shai Almog

Published: 2018-09-10T00:00:00Z

Content type: tutorial

Language: en

Sources: [CodeName One](<https://devfeed.tech/sources/codename-one.md>)

Topics: [WebSocket](<https://devfeed.tech/topics/websocket.md>), [networking](<https://devfeed.tech/topics/networking.md>), [Code](<https://devfeed.tech/topics/code.md>), [App](<https://devfeed.tech/topics/app.md>)

Tags: [app](<https://devfeed.tech/tags/app.md>), [code](<https://devfeed.tech/tags/code.md>), [github](<https://devfeed.tech/tags/github.md>), [networking](<https://devfeed.tech/tags/networking.md>), [retry](<https://devfeed.tech/tags/retry.md>), [update](<https://devfeed.tech/tags/update.md>), [web](<https://devfeed.tech/tags/web.md>), [websocket](<https://devfeed.tech/tags/websocket.md>)

## AI overview

This tip explains how the latest WebSocket cn1lib update can automatically retry a disconnected WebSocket connection every five seconds using autoReconnect(int).

## Source excerpt

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).