Collection.insert with Meteor.methods and latency compensation?

Is it possible to create a meteor method with an insert that works with latency compensation?

Similar to this on the client

const id = collection.insert(..);

What would the equivalent be in a meteor method?

  'doc/insert'(doc) {
     return collection.insert(doc);

Yes - Optimistic UI (aka latency compensation) is one of the main benefits using Methods will give you. Check out the What is a Method and Defining and calling Methods sections of the Guide for more info.

I guess my confusion lies in how the server and the client come to the same _id on both client and server in the case of an insert.

How does meteor do latency compensation but come up with the same id’s for multiple insert’s in the same method call. I was originally operating under the assumption that it wasn’t but testing with

  'collection/insert'(fields) {
    check(fields, Object);
    const id = Collection.insert(fields);
    const id2 = Collection.insert(fields);
    return {id, id2};

Calling this return’s the same id’s on both the client and the server, how?

Well well well! This is a great bit of tech where the random seed is actually shared between server and client, so the set of inserts with generate the same IDs for both executions. We’ve had this since 0.8.1. See more here:

  2. meteor/ at devel · meteor/meteor · GitHub
1 Like

That’s really cool. Thanks, that answers it for me.

1 Like