'How to make a sound when there is an increase in the counter but not a decrease

I am making a call to my firebase database as follows:

//reference from firebase for enquiries
       const Ref = projectDatabase .ref("list").child("enquiries");
            Ref.on("value", (snapshot) => {
  enquiries.value = Object.getOwnPropertyNames(snapshot.val()).length
    });

Everytime a new enquiry comes in (making the length go up by 1) I want to play a sound. But I dont want the sound to play if I delete an enquiry (which makes it go down by 1).

What I tried to implement is :

const sound = ref(new Audio('https://firebasestorage.googleapis.com/v0/b/homejigapp.appspot.com/o/mixkit-positive-notification-951%20(1).wav?alt=media&token=b1452d74-0af3-4da5-b907-c7f94f55d013'));

    //reference from firebase for enquiries
           const Ref = projectDatabase .ref("list").child("enquiries");
                Ref.on("value", (snapshot) => {
      enquiries.value = Object.getOwnPropertyNames(snapshot.val()).length
        sound.value.play();
        });

The issue is that the sound plays every time a user gets on the screen and even when the user rejects an enquiry (so the enquiry length goes down by 1).

Is there a way to make the sound play like an inbox alert (only when a new enquiry comes into the database). A cloud function maybe?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source