Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a stub method in MeteorJS?

What is a stub method in MeteorJS?

Why does including a database call make it a non-stub? Thanks!

like image 470
nubela Avatar asked Jul 07 '13 09:07

nubela


People also ask

What is Meteor methods?

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.

How do I call Meteor method?

// 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.


1 Answers

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.

like image 125
Tarang Avatar answered Oct 01 '22 19:10

Tarang