Socket.IO-Client-Swift
Socket.IO-Client-Swift copied to clipboard
Disconnection
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
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 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
?
@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.
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
I believe I put the close call inside a background task.
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.
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...
I don't know why that would happen
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.
The easiest solution is to just readd the socket when the app comes back into the foreground.
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.
Re-add as in create a new socket
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 - does that happen when the app is in the foreground?
@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
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.
That's why I call .disconnect() on any previous socket instance before creating a new one.
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.
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.
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.
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 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 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.
@liquidg3 hey.. did you find any solution to your problem?
socketmanger.reconnects = false socket.disconnect()
@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.