I’m trying to implement a basic chat messaging server without storing the messages in MongoDB.
This is just to test the functionality, how it works, Here is my implementation.
if(Meteor.isServer){
export const Messages = new Meteor.Collection("messages", {connection: null});
}
//Publish function
if(Meteor.isServer){
Meteor.publish('messages', function(rid = 'ankit') {
let self, finder, finderHandle;
if (!this.userId) {
return this.ready();
}
check(rid, String);
finderHandle = Messages.find({});
//
finderHandle.observe({
added: function (id, fields) {
this.added( 'messages' , id, fields )
}, // Use either added() OR(!) ()
changed: function (id, fields) {
this.changed( 'messages' , id)
},
removed: function (id){
this.removed( 'messages' , id, fields )
}
});
this.ready();
});
}
sendMessage Method
if (Meteor.isServer) {
Meteor.methods({
sendMessage: function (message){
check(message, {
msg: String,
rid: String
});
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
let save = Messages.insert({
msg: message.msg,
rid: message.rid
});
console.log("message saved")
console.log(message);
return {
success: true
};
}
});
}
and on client-side, I’m trying receiving the update but nothing seems to come.
if(Meteor.isClient){
let Messages = new Mongo.Collection('messages')
var user = {username: 'anil'};
Meteor.loginWithKey(user, function (res) {
var message = {
rid: 'ankit',
msg: 'Hi! This is for web client'
};
const handle = Meteor.subscribe('messages');
Tracker.autorun(() => {
const isReady = handle.ready();
console.log(`Handle is ${isReady ? 'ready' : 'not ready'}`);
console.log(handle);
});
Meteor.call('sendMessage', message);
});
console.log(Messages.findOne())
}
On the server-side, I’m getting this Error
Exception in queued task: RangeError: Maximum call stack size exceeded
Login / Sending message Methods are working fine. I’m not sure what I’m missing here, Though I’m able to do all this if I use MongoDB.
Thanks in advance, any comment will be appreciated.