I am building an application using Meteor.js with flashMessage to display informative messages for the user. Currently the message is tied to click events of individual users, but I want to display that message for all users.
Is there a way of using Meteor and flashMessages to accomplish this? Or another package should be used?
Cheers.
There are probably multiple ways, but here is one, using a collection of messages:
Common:
Messages = new Meteor.Collection('messages');
Client:
if (Meteor.isClient()) {
Meteor.subscribe('messages');
var msgs = Messages.find();
msgs.observeChanges({
added: function(id, obj) {
FlashMessages.sendInfo(obj.text);
}
});
}
Server:
if (Meteor.isServer()) {
Meteor.publish('messages', function() {
return Messages.find();
});
}
And then just insert messages like {text: "my text"}
into the Messages
collection and they should be displayed on all clients.
PS: You may want to remove the inserted messages again after a while, or else any newly arriving client will be shown all past messages. Alternatively you could just subscribe to recent messages.