The idea is expose some code to have a reference on how to query and push things to Firebase Realtime Database to create a 1-1 chat with:
- List of channels with me and someone involved.
- Channel with all messages between me and other user
Channel: 1-1 chat
Note: Firebase DB UI is really cool and simple to list Firebase database objects and queries, check it out
Firebase Database usage
Firebase Realtime Database use JSON to define the database structure (yeah, at the end it will be a BIG JSON). So our database will look like:
Check Structure Data Guide
We can query messages, channels and push messages into a channel with the next code:
Note: we used some extensions to make Firebase calls and zip all the results in a easy way (will be better to use Firebase transactions instead ?)
- For mesasges I used
pushfrom Firebase Realtime Database to avoid id conflicts
- If you want to make queries you should use any of
- Because the database is a JSON,
FirebaseDatabase.getInstance().reference.child("messages"), will get a
DatabaseReferencefor all the messages but as the direct child of messages is an id (remember note 1.), you should use
.equalTo("channelIdValue")to get all the children inside messages that have
channelId == "channelIdValue"(query by children of children attributes)
- Databases are created with just users authenticated with firebase auth read/write permissions, but you can make it public for read/write too, it’s ultra insecure, please dont.
- To show the items you can use
Firebase Database UIpackage, check Full post gist for a sample.
Barista Ventures and Oscar Rendon for the support and help.