[SOLVED] Execute method on each connected clients

#1

Hi Meteor Community :slight_smile:

I’m trying to execute a method over all connected clients of my app. It will work on a local network. The thing i want to do : Push notifications on an event.

Actually, i’m doing this :

Template.myTemplate.events({
    'click .btn-primary.toggle-menu': function () {
        Push.create('Something really cool !');
    },
});

Perhaps, this code is only executed on the client which triggered the event. How could i trigger this on each client ?

Note : I’m using this package : asad:push

Thanks in advance :slight_smile:,

Have a productive night,

Brawcks

#2

I’m not familiar with that package. I’d probably just have a collection with events, every client subscribes to listen for a specific set of events. When that event occurs, you insert into the database, all clients are notified of the event, then in an autorun block dependent on a find on this collection, you execute whatever method you’re interested in.

Something like this:

client.js

const Events = Meteor.Collection("events");
Meteor.subscribe("events", {_id: "myEventId"});
Tracker.autorun(() => {
    const event = Events.findOne({_id: "myEventId"});
    if (event) {
       //do something
    }
  });
  Template.myTemplate.events({
    'click .whatever'() {
       Events.insert({_id: "myEventId", arbitraryOtherData: {}});
    }
  });

server.js

const Events = Meteor.Collection("events");
Meteor.publish("events", function (search) {
  return events.find(search);
});
2 Likes
#3

Very nice ! I’ll try this tomorrow after some sleeping hours. Thanks for your time !