observeChanges without initial record

Hi,

I use observeChanges to determine if a new message inside a chat got sent, so I can play a notification sound if the user is not focusing the chat-channel. Everything works fine so far - except one thing. If a user enters the channel the notification audio plays for every document that gets subscribed. I have a limit of 50 messages that get subscribed.

I would like to ask for advice on how I can make observeChanges behave in a way that it will hold back on the initial record and only act if actual new messages are sent.

The function of observeChanges

  OOC_Messages.find({ ooc: true, character: { $not: char }}
    ).observeChanges({
    added() {  
      var oocNow = Session.get("OOC");

      if (oocNow == true) { console.log(ooc) ; } else {

        $('.enterOOC').css('background', 'green');
        var audio = new Audio('/sounds/oocMsg.ogg');
        audio.play();

      }
    },
  });

Thanks for any help!

Check the count example in the official documentation

2 Likes

The flag you are looking for is __supress_initial. But it’s worth noting that if you use redid oplog, it ignores this flag and you get the initial set anyway.