Socket.IO-Client-Swift icon indicating copy to clipboard operation
Socket.IO-Client-Swift copied to clipboard

Disconnection

Open tolgaytoklar opened this issue 9 years ago • 26 comments

Hi

When app goes to background i want to disconnect from server then user returns to app i want to reconnect. If I use disconnect method i am getting "Warning! This socket was previously closed. This might be dangerous!"

What do you suggest for this operation?

I am using Ios 8.4

tolgaytoklar avatar Jul 09 '15 08:07 tolgaytoklar

The only thing I'm aware of that will break if you try and reconnect a closed socket is reconnecting. The thing I did with the app I based this on was to just readd the socket when the client comes from the background.

nuclearace avatar Jul 09 '15 10:07 nuclearace

@nuclearace Hi, I'm trying to find out what is the best-practice to handle socket on iOS once the app is going background

Do you recommend to close the socket once we are getting the applicationDidEnterBackground event ? And to re-create (not calling connect again) the socket once we get applicationDidBecomeActive ?

guyschlider avatar Jul 20 '15 13:07 guyschlider

@mp3il That's what I've done in the past. I haven't had time to research a better alternative that doesn't rely on using Reachability.

nuclearace avatar Jul 20 '15 13:07 nuclearace

But it doesn't closes the connection immediately when application goes to background.

2015-07-20 16:27 GMT+03:00 Erik Little [email protected]:

@mp3il https://github.com/mp3il That's what I've done in the past. I haven't had time to research a better alternative that doesn't rely on using Reachability.

— Reply to this email directly or view it on GitHub https://github.com/nuclearace/Socket.IO-Client-Swift/issues/65#issuecomment-122884243 .

Tolgay Toklar Bursa,Turkey

tolgaytoklar avatar Jul 20 '15 13:07 tolgaytoklar

I believe I put the close call inside a background task.

nuclearace avatar Jul 20 '15 13:07 nuclearace

I'm seeing inconsistent behavior on disconnect. Sometimes I call disconnect and the socket is disconnected in the server immediately. Other times it's not. How can I get consistent behavior from calling disconnect in the client.

2fours avatar Oct 18 '15 21:10 2fours

From time to time the "disconnect" callback is called at random (while the user is navigating the app normally). I added a handler to present a notification to the user and to reconnect. But is this behavior normal? I know the Android version doesn't have to handle this scenario...

dmlebron avatar Dec 14 '15 18:12 dmlebron

I don't know why that would happen

nuclearace avatar Dec 14 '15 18:12 nuclearace

This problem is really important. When app goes to background then comes to foreground, app is not connecting to server immediately. We have to do something for fix this.

tolgaytoklar avatar Dec 27 '15 22:12 tolgaytoklar

The easiest solution is to just readd the socket when the app comes back into the foreground.

nuclearace avatar Dec 27 '15 23:12 nuclearace

What do you mean with “read”? Can you give a little example for this?

28 Ara 2015 tarihinde 01:10 saatinde, Erik Little [email protected] şunları yazdı:

The easiest solution is to just readd the socket when the app comes back into the foreground.

— Reply to this email directly or view it on GitHub https://github.com/nuclearace/Socket.IO-Client-Swift/issues/65#issuecomment-167446948.

tolgaytoklar avatar Dec 27 '15 23:12 tolgaytoklar

Re-add as in create a new socket

nuclearace avatar Dec 27 '15 23:12 nuclearace

I am seeing a similar issue to @davamale. Randomly I'll get my "lost connection" alert that is tied to the "disconnect" event. A client actually asked me today why he sees it even though he's got a good internet connection. I told him to quit and go back in and that fixes it and that I'd research it a bit.

Also, sometimes when I start the app a socket connection is never made. Quitting the app and loading it again makes it work. I've seen this happen across 2 apps.

I will get more debug info together unless anyone has any ideas off the top of their head.

The socket.io-client in the browser is connecting to the same backend and has none of these issues (always stays connected) so I'm confident it's exclusive to this library.

I'll work on narrowing it down until I hear something.

liquidg3 avatar Jan 05 '16 22:01 liquidg3

@liquidg3 - does that happen when the app is in the foreground?

guyschlider avatar Jan 05 '16 22:01 guyschlider

@mp3il yeah. Here is my "com" class, responsible for handling all socket communication. You'll see I deal with connect/disconnect issues by using a callback queue. It works fantastically, but I still get the random disconnects and sometimes no connection at all until I quit and go back in.

Feedback welcome.

https://gist.github.com/liquidg3/05994294b6dc81e14a5e

liquidg3 avatar Jan 05 '16 22:01 liquidg3

The problem with "re-adding" the sockets when the app comes back to the foreground. Is that when it first go to the background, you remove all handlers only, therefore the client will still be connected to the server.

dmlebron avatar Jan 18 '16 16:01 dmlebron

That's why I call .disconnect() on any previous socket instance before creating a new one.

liquidg3 avatar Jan 18 '16 16:01 liquidg3

Also, this only forces a reconnect when an action is taken (see connect() in all the other methods). If the app is sitting socket gets a chance to reconnect on its own.

liquidg3 avatar Jan 18 '16 17:01 liquidg3

Yeah, I know. But in my case I have a Singleton of this class. Therefore I can't call disconnect() (the documentation says: "disconnect() - Closes the socket. Reopening a disconnected socket is not fully tested.", used to say that mustn't be opened after been closed) because I have only one instance throughout the app life-cycle.

dmlebron avatar Jan 18 '16 17:01 dmlebron

I got the same scenario . I have observed that all events those should be received during background are being received when I click on app and run in foreground. The concern really is user get not any kind of notification in background and app is unable to received any call.

I have checked on server and most of times socket is alive. I think this is only issue with listeners on background mode and get all those event as soon as app comes to foreground.

Is that possible to get callbacks in background or should we be looking forward to integration of APNS.

Notes: I have enabled background mode on. I am using this swift library to objective c project.

zaigham-mt avatar May 05 '16 12:05 zaigham-mt

I would not rely on background execution. I've never really tried to get background execution working, other people have said they've had mixed success.

nuclearace avatar May 05 '16 12:05 nuclearace

@nuclearace Socket.IO is holding the the events on background mode. I tried to show local notifications to event. When I disconnect the internet I am getting all holding event local notifications.

zaigham-mt avatar May 19 '16 12:05 zaigham-mt

@zaigham-mt have you got any solution of background disconnection issue? But here is my case I run my app on background the SocketIOClient disconnect after few secs(I run server side on my computer it do disconnect) , When I get to the foreground it connect immediately, but its another socket(different socketId). The background fetch is ON and I m using the lib in Obj-C.

Meonardo avatar Dec 01 '16 01:12 Meonardo

@liquidg3 hey.. did you find any solution to your problem?

kinza88 avatar Jan 02 '18 05:01 kinza88

socketmanger.reconnects = false socket.disconnect()

aoliman avatar Jul 04 '18 14:07 aoliman

@Meonardo Background fetch does not mean it will stay long. If socket is disconnected its oblivious new socket will have new socket id. Issue I was facing is app is in background even if socket is connected, it dont receive the events. I have done for now using APNS. This seems to be issue with Objective C project only , swift is holding events and as soon as app is foreground it get all events. Socket is never disconnected in whole process.

zaigham-mt avatar Jul 04 '18 15:07 zaigham-mt