socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

Socket `disconnect` not fired when device loses network

Open phansen-nd opened this issue 3 years ago • 9 comments

Version 16.0.0

Expected behavior A connected socket will fire the disconnect event when a device loses network. Or fire any event, like statusChanged or reconnect.

Actual behavior No disconnect event is emitted until the ping timeout 60 seconds later.

I've tried the following code to get any indication that the socket is no longer connected:

socket.onAny { event in
     print("Got event: \(event.event), with items: \(event.items)")
}
socket.connect()

but no event fires when the device loses network until 60 seconds later.

For what it's worth, the Java SocketIO client is connected to the same server and receives a disconnect event immediately upon losing network, so it does not seem to be anything to do with the server configuration.

phansen-nd avatar Jun 14 '22 23:06 phansen-nd

Hey! Have you setup all parameters related to reconnection? Eg:

    let config = [
      .log(true),
      .forceWebsockets(true),
      .reconnects(true),
      .reconnectAttempts(5),
      .reconnectWait(1),
      .reconnectWaitMax(10)
    ] as SocketIOClientConfiguration
        
    socketManager = SocketManager.init(socketURL: url, config: config)

It's working properly with above config in my case, so I get several reconnect events and then disconnect.

msobiepanek-uz avatar Jul 08 '22 10:07 msobiepanek-uz

I'm dealing with the same issue. Any update @phansen-nd ?

miguel-arrf avatar Feb 16 '23 00:02 miguel-arrf

@miguel-arrf no we ended up using a different network handler because this didn't work.

phansen-nd avatar Feb 16 '23 00:02 phansen-nd

@miguel-arrf no we ended up using a different network handler because this didn't work.

Oh... @phansen-nd . Thank you anyway!

miguel-arrf avatar Feb 16 '23 00:02 miguel-arrf

This seems to still be an issue... network disconnects causing socket failure should emit a disconnect.

*Edit: When a user loses network connection completely, or switches from Wifi to LTE, it takes about 50-60 seconds for the .error event to be emitted from the SocketManager.defaultSocket. In case of switching network type, it will reconnect using the new network type, but at that 50-60 second delay. Can this be changed to be quicker? ie 2 second delay?

WestFlow127 avatar Nov 03 '23 21:11 WestFlow127

@nuclearace can we get some input on this?

WestFlow127 avatar Nov 09 '23 21:11 WestFlow127

@miguel-arrf no we ended up using a different network handler because this didn't work.

Did you use Apple's Network framework to detect changes in network and force reconnect? @miguel-arrf

WestFlow127 avatar Nov 10 '23 23:11 WestFlow127