Reputation: 761
I am trying to create a one to one chat on a global firebase chat. I was wondering how would I proceed to do it? What I am doing now is that I created another HTML file which is where the private chats would happen and I am searching by users id with this piece of code.
oneOnone.addEventListener('click', function(e){
// Attach an asynchronous callback to read the data at our posts reference
ref.on("value", function (snapshot) {}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
ref.orderByChild("userId").on("child_added", function (snapshot) {
console.log(snapshot.val().userId);
console.log(searchId);
if(searchId.value === snapshot.val().userId){
window.location.href = 'privatemessage.html';
}
});
});
This then leads into the privatemessage.html file where all the chats should happen. The feeling I have been having is that my database might not right.
The database on firebase still registers people as it would in a Global chat, not a 1 on 1 chat. I am just completely confused on how to make sure the chat would be between only two people. If someone could recommend a guide, give a full fledged explanation on how 1v1 chat would work that would be appreciated. Yes I have look at the docs, it really does not explain how to do a 1v1 chat.
Edit
So I have changed up my database to this
So what I am leaning on is something like this for the one on one talk
var pmChat = database.ref('chat/' + userId);
Basically creating a new reference and then linking the userid but now, how I link the userid of the other user?
Upvotes: 1
Views: 2671
Reputation: 1833
you can link current user id with the other userid like this:
database.ref('chat').child(currentUserId).child(otherUserId).child(message);
database.ref('chat').child(otherUserId).child(currentUserId).child(message);
Upvotes: 3