15
Distributed State
In the fourteenth chapter data went from the server to the client: something changed in the database, and a moment later it was visible on screen. But the user changes data too, and then it has to travel the opposite way. The user clicks «add order», the browser sends a request to the server, the server checks permissions, writes the order to the database, answers, and only then does the order appear in the list. If the server is on another continent, this takes a couple of hundred milliseconds, and on a mobile network a whole second. Every click comes with a spinning wait icon, and the application feels slow even though the server is fast.
Meteor decided that there was no need to wait. Among the seven principles it announced in early versions of its documentation was latency compensation: the client simulates what the server will do, and a method call looks as if the server had answered instantly. It works like this. Methods, the functions that change data, are written in shared code that is loaded both on the server and in the browser:
Meteor.methods({
addOrder(title) {
check(title, String);
return Orders.insert({ title, owner: this.userId });
}
});
Meteor.call('addOrder', 'order');
When the client calls a method, Meteor does two things at once. It sends the server a DDP message with the method’s name and arguments, and right away it runs the same method in the browser. The documentation calls this execution a stub or a simulation. The stub writes to Minimongo, Tracker sees the change, and the order appears on screen before the request has even reached the server. Then the server runs the method for real and sends two messages: the result of the call and a notice that all changes caused by this method have already been sent to the client. On receiving the notice, the client throws away everything the stub wrote and keeps what the server sent. If they did the same thing, the user will notice nothing.
It is worse when the stub and the server did different things. The simplest example is the identifier of a new document. The stub in the browser makes it up on its own, the server makes it up on its own too, and the identifiers come out different. The experiment in experiments/13-latency models this scheme in a few dozen lines: the stub inserts a document immediately, the server answers after a hundred and fifty milliseconds, and after the answer the client adopts the server’s state. During reconciliation the document with the stub’s identifier disappears and an identical document with the server’s identifier appears in its place. For the user this is a flicker, and for an interface that keys elements by identifier it also means recreating the element with all its internal state. The DDP specification calls it exactly that, flicker, and describes how Meteor got rid of it. The client sends along with the call a seed for the random number generator, and the server generates the identifier from the same seed with the same algorithm. The identifiers match, and reconciliation changes nothing, which the experiment also shows. In the twelfth chapter Temporal replaced Math.random so that a replay of the code would match the first run. Here the same trick is needed so that two runs of the same method on different machines match.
Gratis, solo falta un paso
El resto del libro es gratis
Deja tu correo y te mando un enlace para confirmarlo. Después del clic, todos los capítulos se abren aquí, en este dispositivo, durante un año. Cuando salga un libro nuevo, escribiré a esa dirección; responde “basta” a cualquier correo y no vuelvo a escribir.
¿Tienes cuenta INITE? Entra y el libro se abre sin correo.
No se cobra nada por el libro. Si quieres apoyarlo, puedes donar desde cualquier capítulo.
Cómo se guarda la dirección