ic. …definitely see how it feels overkill to go through the db to get what you don’t even want to store. …you still could use it, and just observe the messages collection in a publisher, and manually use added
to publish it to subscribed clients. and just delete delete stale messages in an interval that are older than a certain threshold:
Meteor.publish('messages', function() {
Messages.find({}, {sort: {createdAt: -1}, limit: 1}).observeChanges({
added: function(id, doc) {
this.added('messages', id, doc);
}.bind(this)
});
});
Meteor.setInterval(function () {
let threshold = moment().subtract(10, 'minutes').toDate();
Messages.remove({createdAt: {$lt: threshold}});
}, 1000*60*10);
Not that that is anything novel to look at, but you can also use publications/subscriptions like advanced methods (for things like WebRTC signaling or other external requests). Like this:
Meteor.publish('messages', function() {
let self = this;
HTTP.post(WebRTCurl, options, function(res) {
if(res.success) {
self.added('signal_info', res.info.id, res.info); //send to client-only collection (and never persist)
Messages.find({sigId: res.info.id}, {sort: {createdAt: -1}, limit: 1}).observeChanges({
added: function(id, doc) {
self.added('messages', id, doc); //sending to a another collection in same publication (this time backed by a real server side collection)
}
});
}
});
});
Meteor.methods({
sendMessage: function(sigId, type, message) {
if(type == 'im') Messages.insert({text: message, userId: this.userId, sigId});
else if(type == 'disconnect') HTTP.post(WebRTCurl+'/disconnect/'+sigId);
else if //etc
}
);
if(Meteor.isClient) SignalInfos = new Mongo.Collection("signal_info");
I dont how your system all actually works. But the point is if it works for DDP to be the transport format, then you can do a lot more in subscriptions than you might think. Combine that with LiveQuery + Compose.io + a Cluster, and at the very least you won’t have the issue of syncing data between different tools/instances, and perhaps more importantly can code in a very standard Meteor way that will scale horizontally (not infinitely, but very far). You won’t get the throughput and scalability of tools meant just for messaging, but the truth is you will be able to have a wildly successful product before that point comes.