1

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. enter image description here

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 enter image description here

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?

Zubair Amjad
  • 621
  • 1
  • 10
  • 29
  • You'll want to create these rooms in your database. So `/ChatRooms/RoomName1` and then with the messages under that. That way you can secure access to each room based on its participants. For a good way to structure this data, see http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Jun 29 '18 at 03:31

1 Answers1

3

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);
Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48