0

For my 1v1 chat, I am trying to link my userids. I have created a global chat with my database looking like the image below.
enter image description here

This code allows me to search by userid on the firebase database and then pull up the private message html file where the two users will have the private chat.

            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';
                } 
            });
        });

I am leaning towards something like this for the one on one talk var pmChat = database.ref('chat/' + userId);

I would create a new reference and then add the first userid and then I would add the second userid or even the displayname.

How do I get id or displayname I am searching for and then have those two chatting and when each time a user searches, create a new node for each user's chat? And if the a user talks to the same person it goes back to that chat?

Update

I modified the code to look like this

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);
            if (searchId.value === snapshot.val().userId) {
                var currentUser = database.ref('chat').child(userId).child(searchId.value);
                var otherUser = database.ref('chat').child(searchId.value).child(userId);

                messageForm.addEventListener("submit", function(e) {
                    e.preventDefault();

                    var user = auth.currentUser;
                    var userId = user.uid;
                    if (user.emailVerified) {
                        // Get the message the user entered
                        var message = messageInput.value;

                        var myPassword = "11111";
                        var myString = CryptoJS.AES.encrypt(message, myPassword);

                        // Decrypt the after, user enters the key
                        var decrypt = document.getElementById('decrypt')

                        // Event listener takes input
                        // Allows user to plug in the key
                        // function will decrypt the message
                        decrypt.addEventListener('click', function(e) {
                            e.preventDefault();
                            // Allows user to input there encryption password 
                            var pass = document.getElementById('password').value;

                            if (pass === myPassword) {
                                var decrypted = CryptoJS.AES.decrypt(myString, myPassword);
                                document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
                            }
                        });

                        // Create a new message and add it to the list.  
                        if (userId) {

                            currentUser.push({
                                    displayName: displayName,
                                    userId: userId,
                                    pic: userPic,
                                    text: myString.toString(),
                                    timestamp: new Date().getTime(), // unix timestamp in milliseconds

                                })
                                .then(function() {
                                    messageStuff.value = "";

                                })
                                .catch(function(error) {
                                    windows.alert("Your message was not sent!");
                                    messageStuff;
                                });
                        } else {
                            otherUser.push({
                                    displayName: displayName,
                                    userId: userId,
                                    pic: userPic,
                                    text: myString.toString(),
                                    timestamp: new Date().getTime(), // unix timestamp in milliseconds

                                })
                                .then(function() {
                                    messageStuff.value = "";

                                })
                                .catch(function(error) {
                                    windows.alert("Your message was not sent!");
                                    messageStuff;
                                });

I feel I am not grabbing the other userid because it is only the string value of the textbox.

var currentUser = database.ref('chat').child(userId).child(searchId.value);
 var otherUser = database.ref('chat').child(searchId.value).child(userId);

This is how I am creating the new references in the Firebase database.

Community
  • 1
  • 1
Zubair Amjad
  • 621
  • 1
  • 10
  • 29

0 Answers0