2

How to manage concurrent connections of the same user. Is there any way I can limit how many connections my user can keep active?

I could make my database something like:

"users": {
    ...
    "FAKE-USER-ID": {
        "active_connections": 2
    }
    ...
}

And update the key active_connections whenever a user changes their state.

How cases like connection lost and app crashes would be managed in this scenario? Is there a Firebase native way of doing this?

thyago stall
  • 1,654
  • 3
  • 16
  • 30
  • Answer below. But I suspect this may be a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Instead of asking how to limit the connections, it may be better if you tell us *why* you want to limit the connections. – Frank van Puffelen Feb 15 '16 at 14:45

1 Answers1

1

To handle when the user connects, look at .info/connected. You could use this to set a flag when the user connects.

To handle when the user disconnects (either by closing the app or because they lose their connection), look at onDisconnect(). You could this to remove the flag when the user disconnects.

But why would you want to limit the user to only access their data on a single device? Firebase doesn't charge per connection, so why would you want to limit it on that?

A few years ago I was using an app that exhibited this exact behavior and I found myself locked out of the app multiple times per day. I moved over to another app.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Just out of curiosity. I'm coding some toy apps to explore Firebase and got the idea of a poor monetizing strategy :) – thyago stall Feb 15 '16 at 15:31
  • As a curiosity I like it btw. As a monetization strategy, not so much. ;-) – Frank van Puffelen Feb 15 '16 at 15:34
  • There are definitely cases where restricting simultaneous logins is ideal. If, for example, the user is deeply embedded in a part of the UI where they're required (by the app) to obtain access and then on another device they remove that access but are still deeply embedded in the first device, things can get ugly. The complexity of safeguarding against this on the client can become incredibly ugly. And if the app just wants 1 device to represent 1 person with 1 location (in a phone-only app with a UX where user location is vital), blocking simultaneous logins (of the <1%) is not concerning. – trndjc Nov 23 '19 at 18:19