Search code examples
javascriptangularjsmeteorangular-meteor

How to play an audio to notify when new record is inserted in collection(cursor.observeChanges) in Meteor


I have a meteor app where I was trying to notify on client side when new record is entered in to the collection.For this I tried using cursor.observeChanges and able to give a notification message on client side..But additional to this I need play small audio or an alert so that we can easily identify it .I tried but it was not working

var cursor = customerDetails.find();
    cursor.observeChanges({
    added: function(id, object) {
   Notification.error('NEW Record');
   var audio = new Audio('audio.mp3');
    audio.addEventListener('cursor.observeChanges', function() {
        console.log("Inside Audio");
    audio.play();
    });
     }
    });

I saved the audio file in public folder.Can someone help how can i play this


Solution

  • You are adding an event listener every time the record is added, but the listener does not get triggered. Instead, you should just run audio.play() whenever the record is added.

    var audio = new Audio('audio.mp3');
    var cursor = customerDetails.find();
    cursor.observeChanges({
        added: function(id, object) {
            Notification.error('NEW Record');
            console.log("Record added")
            audio.play();
        }
    });