What is a stub method in MeteorJS?
Why does including a database call make it a non-stub? Thanks!
Meteor methods are functions that are written on the server side, but can be called from the client side. On the server side, we will create two simple methods. The first one will add 5 to our argument, while the second one will add 10.
// Asynchronous call Meteor. call('foo', 1, 2, (error, result) => { ... }); If you do not pass a callback on the server, the method invocation will block until the method is complete. It will eventually return the return value of the method, or it will throw an exception if the method threw an exception.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With