Here is an example of how you can use publish and subscribe without interacting with a database.
In this case I will also ignore the need to have a client-side collection, a simple object will be used instead, a collection name and an id are informed because of the API rules, don’t create it.
An EventEmitter came up by chance, use whatever font you think best.
It would be better to use Meteor.call(‘newMessage’ {to, message}) and use Meteor’s internal routines to access the active connections, but I don’t remember now, but they do exist.
Some adjustment to this code needs to be done, it was a memory I had along with something ready-made I had when I read your question here on the forum, English is not my language and I’m not on a computer that allows me to test, but the basic idea are you there
/server/publish.js
import EventEmitter from 'events';
import { Meteor } from 'meteor/meteor'
const chats = {};
const chatEvents = new EventEmitter();
chatEvents.on('newMessage', ({from, to, message}) => {
chats[to].added(
'chatMessages', // a collection name
someUniqueId, // id
{from, message}
);
})
Meteor.publish('chat', function(name) {
chats[name] = this;
this.ready();
});
/client/subscribe.js
import {Meteor} from 'meteor/meteor'
const obj = [
'update', //gotcha!
'beginUpdate',
'endUpdate',
'saveOriginals',
'retrieveOriginals',
'getDoc',
'_getCollection',
].reduce((a, d) => {
a[d] = (...args) => {
console.log(d, args);
};
return a;
}, {});
obj.update = ({ msg, collection, id, fields }) => {
switch (msg) {
case 'added':
console.log('A new message!', fields) // {from, message}
break;
default:
console.log('other event', { msg, collection, id, fields });
}
};
Meteor.subscribe('chat', MyUniqueName, function(){
this.connection.registerStore('chatMessages', obj);
});