I have some difficulties with structuring the data for Firebase. I have uses and rooms. Each user can join multiple rooms and each room can have multiple users.
This is my setup right now:
I save all the room id's at the linked device (room_info). All linked devices are under the key -> device_info at the room child. I save the device name there for fast lookup.
I have used this webbpage to validate my idea: https://www.firebase.com/docs/web/guide/structuring-data.html
Is there something that can be done better?
And I want to save the device state (if devices are online). I want to create a device_state child under the room key. And I want to save all device id's there that are online. And remove them when offline. Is that a good approach?
You need to know that there is no perfect solution for structuring a Firebase database. The best solution, is the solution that fits your needs and makes your job easier. Having in mind that everything is for the view, i suggest you a few things to remember.
Depth is not a factor that affects speed on a technical level. Firebase can as quickly look up a node at level 30 as it can at level 1. The by fast major contributing factors to performance are the
size of the data
your app reads/writes and theavailable bandwidth
.Denormalization is a common practice within Firebase.
If we speak about scalability we need also to keep in mind that an important rule in firebase is to have the data as flatten as possible. Please see the following post, Structuring your Firebase Data correctly for a Complex App, for a better understanding.
However, keeping the data flat is usually better depending on your use case. So don't go overcomplicating your structure if you don't need to. Don't use denormalization just for your sake for having the database denormalized.
Regarding your actual Firebase database structure, if you can query it easily to get the desired data, then go ahead with it. If you want more information, i suggest you visit the links above.
If you have a SQL background, i also suggest you see this tutorial, The Firebase Database For SQL Developers.
Regarding your last question, yes, it's a very good approach.
Hope it helps.