What is a stub method in MeteorJS?
Why does including a database call make it a non-stub? Thanks!
I think you mean the ones referred to in the docs? The stubs are the ones defined via Meteor.methods
.
In Meteor these stubs allow you to have latency compensation. This means that when you call one of these stubs with Meteor.call
it might take some time for the server to reply with the return value of the stub. When you define a stub on the client it allows you to do something on the client side that lets you simulate the latency compensation.
I.e I can have
var MyCollection = new Meteor.collection("mycoll")
if(Meteor.isClient) {
Meteor.methods({
test:function() {
console.log(this.isSimulation) //Will be true
MyCollection.insert({test:true});
}
});
}
if(Meteor.isServer) {
Meteor.methods({
test:function() {
MyCollection.insert({test:true});
}
});
}
So documents will be inserted on both the client and the server. The one on the client will be reflected 'instantly' even though the server has not replied with whether it has been inserted or not.
The client side stub allows this to happen without having two documents inserted even though insert is run twice.
If the insert fails, the server side one wins, and after the server responds the client side one will be removed automatically.
For the above code you can write this that will be ran on both server and client, use isSimulation to identify on wich side you are if you need to do specific task :
var MyCollection = new Meteor.collection("mycoll")
Meteor.methods({
test:function() {
console.log(this.isSimulation) //Will be true on client and false on server
var colItem = {test:true, server: true};
if (this.isSimulation) {
colItem.server = false;
}
MyCollection.insert(colItem);
}
});