Hello,
I've been working with Firebase's realtimedatabase this week. There is no example yet of how to create rooms and send messages, but it is important for anyone who wants to make a chat in his App, for this reason I make an example of it.
I oriented myself on this example. And thanks DonManfred for his example project.
This example is maskless and purely for the Firebase structure.
The result of this exmaple/tutorial is this:
The room name is "Fußball" (football)
He becomes automatically a id (child)
Based on this id we can enter the room and send messages in there.
subordinate are the messages. Every message gets its own generated id.
The "senderId" is the FireBase Auth uid from the user. This is important, by which we can later assign from whom came the message. Last but not least is the "text" this is the message.
Let's get to the code:
I show only the technical behind it when you press the button, for the input is everyone responsible himself!
i use:
Firebase RealTimeDatabase lib.
FireBase AuthEx
I hope you continue to help, for tips or questions I'm open.
I've been working with Firebase's realtimedatabase this week. There is no example yet of how to create rooms and send messages, but it is important for anyone who wants to make a chat in his App, for this reason I make an example of it.
I oriented myself on this example. And thanks DonManfred for his example project.
This example is maskless and purely for the Firebase structure.
The result of this exmaple/tutorial is this:
The room name is "Fußball" (football)
He becomes automatically a id (child)
Based on this id we can enter the room and send messages in there.
subordinate are the messages. Every message gets its own generated id.
The "senderId" is the FireBase Auth uid from the user. This is important, by which we can later assign from whom came the message. Last but not least is the "text" this is the message.
Let's get to the code:
I show only the technical behind it when you press the button, for the input is everyone responsible himself!
i use:
Firebase RealTimeDatabase lib.
FireBase AuthEx
B4X:
Sub Process_Globals
'it is better to put it in to the starter service, not on main!
Dim auth As FirebaseAuthEx
Dim realtime As FirebaseDatabase
End Sub
Sub Globals
Dim roomref As DatabaseReference
Dim messageref As DatabaseReference
Dim childOftheOnlyRoom as String
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("frm_main")
auth.Initialize("auth")
auth.signInAnonymously 'signIn to Firebase Anonym
realtime.Initialize("Realtime")
realtime.PersistenceEnabled = True
realtime.goOnline
roomref.Initialize("room",realtime.getReference2("/rooms"),"room") 'is the url to the firebase database "/rooms" is the path of all rooms.
roomref.addChildEventListener 'not relevant for this tutorial
roomref.addListenerForSingleValueEvent 'this too
roomref.keepSynced(True)
End Sub
Sub room_onChildAdded(Snapshot As Object, child As String, tag As Object)
childOftheOnlyRoom = child
end sub
Sub btn_createRoom_Click
'with this we are create a room with a generatet id and a customized name.
roomref.push.setValue(CreateMap( "name" : "The Name of the Room","newroom1", "post")
roomref.orderByKey
End Sub
Sub btn_sendMessage_Click
'here we are send a message in this room on his generatet id, we find this room_id on the onChildAdded listener as child, store this child in to your local database or on a variable.
'we need the room_id and under this id, we put "/messages" for the assignment
messageref.Initialize("messages", realtime.getReference2("/rooms/" & childOftheOnlyRoom & "/messages") ,"message")
Dim messagesitems As Map = CreateMap("senderId": auth.CurrentUser.Uid, "text": "Test Message 1")
messageref.push.setValue(messagesitems,"rooms","messages")
End Sub
I hope you continue to help, for tips or questions I'm open.
Last edited: