Running a global function that runs for all connected users upon event


#1

I’m wanting to run a function upon a click event for all clients that displays a toast message and also plays a sound.

One solution that I’ve found to work is to define an object in a collection, create a Meteor.setInterval function and occasionally tick a certain amount of time to check for changes in the database. I’d prefer if this was reactive, however.

Is there a more elegant solution to doing this?


#2

You can use observe() or observeChanges() :slight_smile:

Meteor.subscribe('messages')
Messages.observeChanges({
  added(id, message){
    alert(message.text)
  }
})

#3

Is there a way to globally emit server events (like socket.io) instead of relying on solely on the database?


#4

Yes, by creating a “manual” publication, using this.added, that publishes to a client-only collection