I'm trying to implement my own custom ping-pong for my meteor application (meteor has a ping-pong implemented for internal use, but I want to introduce a custom behavior). The only way that I found to send data to a client was by using a publisher, so I set a setInterval
, to send a new payload with the timestamp of the pong, but in each new subscription is created a new setInterval
. How could I set just a one publish or setInterval? Or there some another way to send a message to client?
const COLLECTION_NAME = 'ping-pong';
const POLL_INTERVAL = 5000;
function pingPong(credentials) {
const pongSender = () => {
const id = _.uniqueId('pong-');
const payload = {
message: 'pong',
time: Date.now(),
};
this.added(COLLECTION_NAME, id, payload);
};
pongSender();
this.ready();
const interval = Meteor.setInterval(pongSender, POLL_INTERVAL);
this.onStop(() => {
Meteor.clearInterval(interval);
});
}