3

I have a lobby in which I want the users to be in sync. So when a user turns off his internet while the app is running, he should be removed. I know Firebase does not support server side coding, so the coding needs to be client side. The answers from How to delete firebase data after "n" days and Delete firebase data older than 2 hours do not answer this question since they expect that the user is online and they have an internet connection. So my question is if is possible to delete users when they got no internet? I thought maybe it is an idea to let the users update a value every 5 seconds, and when that update is not done, the other users in that lobby remove the player. This way is not good, since every player needs to retrieve and upload alot of data every 5 seconds. What is the best way to solve this?

Edit: to make it short, lets say each user has an image. The image should be green when the user is connected, and grey when disconnected.

Edit 2: after thinking it over, it is really hard to accurate present the connected users on a client-side server. That is why, if nobody has a different solution, I should add another server which can execute server-side codes. Because of the larges amount of servers, I would like to know which server I should use. The server should run a simple function which only checks if the users are connected or disconnected and can communicate with Firebase. If I am correct it should look like this: servers

But the server also needs to communicate with the users directly. I have absoluty no idea where to start.

Community
  • 1
  • 1
NoKey
  • 129
  • 11
  • 32
  • You may try this https://firebase.google.com/docs/database/ios/offline-capabilities#section-connection-state – Dmytro Rostopira Mar 01 '17 at 09:37
  • I knew about that option. But how is the user supposed to upload data to the server, if they got no internet? I know the function will execute when the user reenables internet, but this is not the best way. – NoKey Mar 01 '17 at 09:41
  • this function registers callback on server, so when connection lost - it will be executed. But due to iOS restrictions, connection lost as soon, as user leaves app – Dmytro Rostopira Mar 01 '17 at 10:37
  • 1
    >When you establish an onDisconnect() operation, the operation lives on the Firebase Realtime Database server. The server checks security to make sure the user can perform the write event requested, and informs the your app if it is invalid. The server then monitors the connection. If at any point the connection times out, or is actively closed by the Realtime Database client, the server checks security a second time (to make sure the operation is still valid) and then invokes the event. – Dmytro Rostopira Mar 01 '17 at 10:40
  • When I close the app, the print "not connected" is not shown. This value needs to execute when the user has no internet: self.channelRef?.child(self.userKey).removeValue(). I now have that function inside the print which will show not connected. When I turn on airplane mode, I see the print not connected, but the value is not removed. When turning on the internet, the value removes. What I really want, is that the value will be removed directly when there is no connection. – NoKey Mar 01 '17 at 11:10
  • Maybe it is a good solution to check the connection for other users, rather than checking the connection for the current user. Is it possible to check if other users are connected? – NoKey Mar 01 '17 at 11:24
  • iOS suspend all app connections, when it's in background. The only way is to use silent push message user-to-user. But it's a bit tricky – Dmytro Rostopira Mar 01 '17 at 11:56
  • 1
    "each user has an image. The image should be green when the user is connected, and grey when disconnected" The presence app in the Firebase documentation does precisely that: https://firebase.google.com/docs/database/ios/offline-capabilities#section-sample – Frank van Puffelen Mar 01 '17 at 15:05
  • If I got 1 user which comes online and then turn on airplane mode, how will the value be set to the last timpstamp when he was seen online? – NoKey Mar 01 '17 at 15:57
  • @JasperVisser you may want to check the edit i made on the answer i gave! – i6x86 Mar 02 '17 at 16:28

1 Answers1

3

If I'm not completely wrong, you should be able to use onDisconnect.

From the Firebase, documentation:

How onDisconnect:Works: When an onDisconnect() operation is established, it lives on the Firebase Realtime Database server. The server checks security to make sure the user can perform the write event requested, and informs the client if it is invalid. The server then monitors the connection. If at any point it times out, or is actively closed by the client, the server checks security a second time (to make sure the operation is still valid) and then invokes the event.

In app in production I'm using onDisconnectRemoveValue, and when I close the app, the user removes himself from the lobby. Not sure how it works when you turn the device in airplane mode, but from the documentation it seems there should be no problem.

One thing: when you test it better do it on real device, the simulator have issues with turning it off and on, at least the on I have installed.

Edit: So i checked the onDisconnect when you put the device on airplane mode and it works! The question is, that it removes the user in about a 1:30 min, approximately, so if you read the documentation or ask the support, you may be (and only may be) able to find a way to set the time you want.

i6x86
  • 1,557
  • 6
  • 23
  • 42
  • Yyes this was the struggle, I did not know there was a delay with the airplane mode... Thank you! – NoKey Mar 02 '17 at 18:06