Fork me on GitHub

Guide for client developers

This guide shows how you can use JMAP to efficiently minimise bandwidth usage and round trips whilst still giving the user a complete view of their mail. This is particularly relevant to clients on mobile devices, where there is not the space to store a complete cache of all messages, or for web clients where you often do not have a permanent cache at all and must efficiently start from scratch each time the app is loaded.

A single login may have access to multiple accounts. In this guide I’m going to ignore this and just use the primary account, which I’m going to presume has full read-write mail access.

Cold boot

When a user first logs in and you have no data cached for them, first call:

[
    [ "getMailboxes", {}, "#0" ]
]

This will fetch the list of mailboxes for the user, and the permissions and roles for each. Here’s an example of the response you might receive:

[
    [ "mailboxes", {
        accountId: 'user@example.com',
        state: "m123456789",
        list: [
            {
                id: "mailbox1",
                name: "Inbox",
                parentId: null,
                role: "inbox",
                mustBeOnlyMailbox: false,
                mayAddMessages: true,
                mayRemoveMessages: true,
                mayCreateChild: false,
                mayRenameMailbox: true,
                mayDeleteMailbox: false,
                totalMessages: 1424,
                unreadMessages: 3,
                totalThreads: 1213,
                unreadThreads: 2
            },
            {
                id: "mailbox2",
                name: "Sent",
                parentId: null,
                role: "sent",
                mustBeOnlyMailbox: false,
                mayAddMessages: true,
                mayRemoveMessages: true,
                mayCreateChild: false,
                mayRenameMailbox: true,
                mayDeleteMailbox: false,
                totalMessages: 41,
                unreadMessages: 0,
                totalThreads: 32,
                unreadThreads: 2
            },
            {
                id: "mailbox3",
                name: "Trash",
                parentId: null,
                role: "trash",
                mustBeOnlyMailbox: true,
                mayAddMessages: true,
                mayRemoveMessages: true,
                mayCreateChild: false,
                mayRenameMailbox: true,
                mayDeleteMailbox: false,
                totalMessages: 3,
                unreadMessages: 0,
                totalThreads: 2,
                unreadThreads: 0
            },
            {
                id: "mailbox4",
                name: "Awaiting Reply",
                parentId: "mailbox2",
                role: null,
                mustBeOnlyMailbox: false,
                mayAddMessages: true,
                mayRemoveMessages: true,
                mayCreateChild: true,
                mayRenameMailbox: true,
                mayDeleteMailbox: true,
                totalMessages: 0,
                unreadMessages: 0,
                totalThreads: 0,
                unreadThreads: 0
            }
        ],
        notFound: null
    }, "#0"]
]

In this (simple) example, the user has four mailboxes. Three at the top level (Inbox, Sent and Trash) and one submailbox of Sent called “Awaiting Reply”. The first 3 have role attributes, so they are to be used for the designated system roles (these are much as you would expect, but see the spec for full details). Note, you should always use the role attribute, as names may be localised (or even different between different servers with the same language)!

The Inbox, Sent and Awaiting Reply mailboxes all have mustBeOnlyMailbox:false. The Trash mailbox on the other hand has mustBeOnlyMailbox:true. This means that a message in the trash may not be in any other mailbox, and vice versa. However, a message belonging to the Sent mailbox could also be in, for example, the Awaiting Reply mailbox.

The Sent mailbox has no unread messages, but 2 unread threads. This is not an error! A thread is considered unread if any of the messages in it are unread, even if those messages are actually in a different mailbox. In this case, the 3 unread messages in the Inbox must all be replies to messages in the Sent mailbox. Email clients will probably want to hide unread thread counts for mailboxes with a role of “sent” or “archive”.

Presuming the client defaults to showing the role=inbox mailbox, we now get the list of messages at the top of the mailbox, and the data for each one that we need to show it in the interface.

[
    [ "getMessageList", {
        filter: {
            inMailboxes: [ "mailbox1" ]
        },
        sort: [ "date desc", "id desc" ]
        collapseThreads: true,
        position: 0,
        limit: 10,
        fetchThreads: true,
        fetchMessages: true,
        fetchMessageProperties: [
            "threadId",
            "mailboxId",
            "isUnread",
            "isFlagged",
            "isAnswered",
            "isDraft",
            "hasAttachment",
            "from",
            "to",
            "subject",
            "date",
            "preview"
        ],
        fetchSearchSnippets: false
    }, "call1"]
]

This might return the following:

[
    [ "messageList", {
        accountId: 'user@example.com',
        filter: {
            inMailboxes: [ "mailbox1" ]
        },
        sort: [ "date desc", "id desc" ]
        collapseThreads: true,
        state: "m8231u341",
        canCalculateUpdates: true,
        position: 0,
        total: 1213,
        messageIds: [
            "fm1u314",
            "fm1u312",
            "fm1u298",
            "fm1u286",
            "fm1u265",
            "fm1u254",
            "fm1u241",
            "fm1u211",
            "fm1u109",
            "fm1u3"
        ],
        threadIds: [
            "4f512aafed75e7fb",
            "fed75e7fb4f512aa",
            "75e7fb4f512aafed",
            "512aafed75e7fb4f",
            "fb4f512aafed75e7",
            "2aafed75e7fb4f51",
            "afed75fb4f512ae7",
            "e7fb4f512aafed75",
            "ed75e74f51fb2aaf",
            "b4f5ed75e712aaff"
        ]
    }, "call1" ],
    [ "threads", {
        accountId: 'user@example.com',
        state: "mc1264092",
        list: [{
            id: "4f512aafed75e7fb",
            messageIds: [ "fm1u314" ]
        }, {
            id: "fed75e7fb4f512aa",
            messageIds: [ "fm1u312", "fm2u12", "fm1u304" ]
        },
            … 8 more thread objects, omitted for brevity …
        ],
        notFound: null
    }],
    [ "messages", {
        accountId: 'user@example.com',
        state: "m815034",
        list: [{
            "id": "fm1u314",
            "threadId": "4f512aafed75e7fb",
            "mailboxIds": [ "mailbox1" ],
            "isUnread": true,
            "isFlagged": false,
            "isAnswered": false,
            "isDraft": false,
            "hasAttachment": false,
            "from": [
                { name: "Joe Bloggs", email: "joebloggs@fastmail.fm" }
            ],
            "to": [
                { name: "Jane Doe", email: "janedoe@fastmail.fm" }
            ],
            "subject": "Camping trip",
            "date": "2014-07-24T15:04:51Z",
            "preview": "Hey Joe. Fancy a trip out west next week? I hea…"
        },
        {
            "id": "fm1u312",
            "threadId": "fed75e7fb4f512aa",
            "mailboxIds": [ "mailbox1" ],
            "isUnread": false,
            "isFlagged": true,
            "isAnswered": false,
            "isDraft": false,
            "hasAttachment": true,
            "from": [
                { name: "James Connor", email: "jamesconnorwork@fastmail.fm" }
            ],
            "to": [
                { name: "Jane Doe", email: "janedoe@fastmail.fm" },
                { name: "Joe Bloggs", email: "joebloggs@fastmail.fm" }
            ],
            "subject": "Re: I need the swallow velocity report ASAP",
            "date": "2014-07-24T12:01:43Z",
            "preview": "Come on you guys. How long can it take to do a simple scientif…"
        },
        {
            "id": "fm2u12",
            "threadId": "fed75e7fb4f512aa",
            "mailboxIds": [ "mailbox2" ],
            "isUnread": false,
            "isFlagged": false,
            "isAnswered": false,
            "isDraft": false,
            "hasAttachment": false,
            "from": [
                { name: "Jane Doe", email: "janedoe@fastmail.fm" }
            ],
            "to": [
                { name: "James Connor", email: "jamesconnorwork@fastmail.fm" },
                { name: "Joe Bloggs", email: "joebloggs@fastmail.fm" }
            ],
            "subject": "Re: I need the swallow velocity report ASAP",
            "date": "2014-07-24T11:32:15Z",
            "preview": "It's on its way. Jane."
        },
            … more message objects, omitted for brevity …
        ],
        notFound: null
    }, "call1" ]
]

We now have the header information for all the messages in the top 10 threads in the Inbox, plus a full list of mailboxes and their unread counts. Providing the screen cannot fit more than 10 messages without scrolling, this is sufficient to display a standard mail interface as though all data was loaded, even though we have only made 2 round trips to the server, and transferred minimal data. (Obviously, you can request more than 10 in the request if you need more to show the initial screen).

Paging in data as the interface is navigated

In our example Inbox, there are 1213 threads, but so far we have only loaded in data for the first 10. As the user scrolls down, we need to page in the data for the section of the mailbox that becomes visible (and indeed, to avoid the user having to wait, it’s advisable to preload a little way ahead too). This is just another call to getMessageList, as in the cold boot example, but with the position property changed to the index for the section required.

Similarly, if we switch mailboxes, or want to do a search, we can use the same call as well. However, remember in JMAP the same message may appear in multiple mailboxes, so to avoid downloading the same data multiple times, it’s advisable to just fetch the message list without also getting the message or thread objects (except in certain situations, such as the very first request; the exact heuristics for deciding between the two can be made arbitrarily clever and complex).

[
    [ "getMessageList", {
        filter: {
            inMailboxes: [ "mailbox2" ]
        },
        sort: [ "date desc", "id desc" ]
        collapseThreads: true,
        position: 11,
        limit: 10,
        fetchThreads: false,
        fetchMessages: false,
        fetchMessageProperties: null,
        fetchSearchSnippets: false
    }, "call1"]
]

After the list has been returned, a second request can be made directly to getThreads with the thread ids (or to getMessages with the message ids) to fetch the ones you are missing.

Opening a thread

So far we have only fetched the minimal amount of information we need to display a mailbox entry for a message or thread. When opening a thread from the mailbox, we now need to fetch the rest of the message details. Exactly what you wish to fetch depends on what information your client displays. Suppose we open the 2nd thread in the example Inbox above. I’m presuming we already know the list of message ids in the thread from the data received when we fetched the mailbox.

[
    [ "getMessages", {
        ids: [ "fm1u312", "fm2u12", "fm1u304" ],
        properties: [
            "threadId",
            "mailboxIds",
            "isUnread",
            "isFlagged",
            "isAnswered",
            "isDraft",
            "hasAttachment",
            "from",
            "to",
            "cc",
            "bcc",
            "replyTo",
            "subject",
            "date",
            "size",
            "body",
            "attachments",
            "attachedMessages"
        ]
    }]
]

Alternatively, a client may want to just request the “rawUrl” property, then download the original RFC2822 message from the URL returned and parse it in the client.

Staying in sync

Suppose a new message arrives, or the user performs some actions on their messages using a different client. A push notification comes in to indicate the state has changed on the server, or you get a response back to a method call with a different state string to the previous call. We now need to efficiently work out exactly what has changed so that we can update the model in the client and keep it in sync, without throwing away all of our data and starting again.

To efficiently stay in sync, we can call:

[
    [ "getMailboxUpdates", {
        sinceState: "m123456789",
        fetchRecords: true,
        fetchRecordProperties: null
    }, "call1" ],
    [ "getMessageListUpdates", {
        filter: {
            inMailboxes: [ "mailbox1" ]
        },
        sort: [ "date desc", "id desc" ]
        collapseThreads: true,
        sinceState: "m8231u341",
        uptoMessageId: "fm1u3",
        maxChanges: 100
    }, "call2" ],
    [ "getMessageUpdates", {
        sinceState: "m815034"
        maxChanges: 30,
        fetchRecords: true,
        fetchRecordProperties: [
            "threadId",
            "mailboxIds",
            "isUnread",
            "isFlagged",
            "isAnswered",
            "isDraft",
            "hasAttachment",
            "labels",
            "from",
            "to",
            "subject",
            "date",
            "preview"
        ]
    }, "call3" ],
    [ "getThreadUpdates", {
        sinceState: "mc1264092"
        maxChanges: 20,
        fetchRecords: true
    }, "call4" ]
]

This is a good example of multiple method calls combined into a single request. Let’s have a look at what they are doing.

  1. getMailboxUpdates: This will return the ids of any mailboxes that have been created or modified (e.g. renamed, or have changed counts). It will also return the ids of any mailboxes that have been deleted. Because we set fetchMailboxes == true, any new or changed mailboxes will be fetched. If the server supports the onlyCountsChanged property of the mailboxes response, then we will only receive the counts properties for the changed mailboxes if that is the only change (the common case).
  2. getMessageListUpdates: All our message lists may have changed, so we must mark all of them as needing a refresh, and immediately refresh the one that is currently selected in the client (here, presuming the Inbox). We’ll look at the response to this and how it is used to update the client cache below.
  3. getMessageUpdates: Gets the list of ids for all messages which have been added or modified, plus the list of messages which have been deleted. We have also specified fetchMessages and fetchMessageProperties arguments here to request the standard header information we need to display a message in a mailbox for any new/changed messages. This includes all mutable properties of a message, so is sufficient to bring any changed messages up to date.
  4. getThreadUpdates: Gets the list of threads which have had messages added or removed from the thread. We also fetch the Thread object for those threads that have changed.

In each case, the sinceState argument comes from the response to our previous calls to get the records of that type.

Handling a standard response

In the common case, not many changes will have occurred since we last synced with the server, and the data returned from this request will be sufficient to fully bring the client into sync with the server.

The maxChanges argument prevents methods from returning huge volumes of data in the case where a large number of changes have been made. If we get a tooManyChanges error in response to one of our method calls then we will then have to do more work to get back in sync, as detailed below.

Let’s look at a common case example, where two new messages have been delivered to the Inbox and one other existing message has been marked as read. We might get back a response something like:

[
    [ "mailboxUpdates", {
        accountId: 'user@example.com',
        oldState: "m123456789",
        newState: "m123471231",
        changed: [ "mailbox1" ],
        removed: [],
        onlyCountsChanged: true
    }, "call1" ],
    [ "mailboxes", {
        accountId: 'user@example.com',
        state: "m123471231"
        list: [{
            totalMessages: 1426,
            unreadMessages: 4,
            totalThreads: 1214,
            unreadThreads: 3
        }],
        notFound: null
    }, "call1" ],
    [ "messageListUpdates", {
        accountId: 'user@example.com',
        filter: {
            inMailboxes: [ "mailbox1" ]
        },
        sort: [ "date desc", "id desc" ]
        collapseThreads: true,
        oldState: "m8231u341",
        newState: "m8239u342",
        uptoMessageId: "fm1u3",
        removed: [{
            messageId: "fm1u241",
            threadId:
        }],
        added: [{
            messageId: "fm1u316",
            threadId: "afed75fb4f512ae7",
            index: 0
        }, {
            messageId: "fm1u315",
            threadId: "b4aae3925af0a0a2",
            index: 1
        }],
        total: 1214
    }, "call2" ],
    [ "messageUpdates", {
        accountId: 'user@example.com',
        oldState: "m815034",
        newState: "m815039",
        changed: [ "fm1u316", "fm1u315", "fm1u314" ]
        removed: []
    }, "call3" ],
    [ "messages", {
        accountId: 'user@example.com',
        state: "m815039",
        list: [{
            "id": "fm1u316",
            "threadId": "afed75fb4f512ae7",
            "mailboxId": "mailbox1",
            "isUnread": true,
            "isFlagged": false,
            "isAnswered": false,
            "isDraft": false,
            "hasAttachment": false,
            "from": [
                { name: "Joe Bloggs", email: "joebloggs@fastmail.fm" }
            ],
            "to": [
                { name: "Jane Doe", email: "janedoe@fastmail.fm" }
            ],
            "subject": "Camping trip",
            "date": "2014-07-24T15:04:51Z",
            "preview": "Hey Joe. Fancy a trip out west next week? I hea…"
        }, {
            "id": "fm1u315",
            "threadId": "b4aae3925af0a0a2",
            "mailboxId": "mailbox1",
            "isUnread": true,
            "isFlagged": false,
            "isAnswered": false,
            "isDraft": false,
            "hasAttachment": false,
            "from": [
                { name: "Joe Bloggs", email: "joebloggs@fastmail.fm" }
            ],
            "to": [
                { name: "Jane Doe", email: "janedoe@fastmail.fm" }
            ],
            "subject": "Camping trip",
            "date": "2014-07-24T15:04:51Z",
            "preview": "Hey Joe. Fancy a trip out west next week? I hea…"
        }, {
            "id": "fm1u314",
            "threadId": "4f512aafed75e7fb",
            "mailboxId": "mailbox1",
            "isUnread": false,
            "isFlagged": false,
            "isAnswered": false,
            "isDraft": false,
            "hasAttachment": false,
            "from": [
                { name: "Joe Bloggs", email: "joebloggs@fastmail.fm" }
            ],
            "to": [
                { name: "Jane Doe", email: "janedoe@fastmail.fm" }
            ],
            "subject": "Camping trip",
            "date": "2014-07-24T15:04:51Z",
            "preview": "Hey Joe. Fancy a trip out west next week? I hea…"
        }],
        notFound: null
    }, "call3" ],
    [ "threadUpdates", {
        accountId: 'user@example.com',
        oldState: "mc1264092",
        newState: "mc1264097",
        changed: [ "afed75fb4f512ae7", "b4aae3925af0a0a2" ],
        removed: []
    }, "call4" ]
    [ "threads", {
        accountId: 'user@example.com',
        state: "mc1264097",
        list: [{
            id: "afed75fb4f512ae7",
            messageIds: [ "fm1u241", "fm1u316" ]
        }, {
            id: "b4aae3925af0a0a2",
            messageIds: [ "fm1u315" ]
        }],
        notFound: null
    }, "call4" ]
]

Here’s how we apply this information to our current state to stay in sync:

  1. mailboxUpdates/mailboxes: The inbox is the only mailbox to have changed, and only the counts have changed. The new counts are returned in the mailboxes response, so we just need to update our cache with the new properties to bring us fully in sync.
  2. messageListUpdates: This is more interesting.

    Suppose the client has a sparse list of messageIds (for example, the user opened the mailbox, loading the first section, then jumped to the middle):

     [ 'm1u1', 'm1u2', 'm1u3', 'm1u4', -, -, -, 'm1u8', -, ...]
    

    To update this to match the new server state:

    1. Check the oldState property matches the current state of the list. Also check the sort and search are the same. If any of these don’t match, abort and re-request an update from the actual current state.
    2. If the newState property is the same as the current state of the list, nothing to do, so return.
    3. If there’s an uptoMessageId, search for this id. If found, remove anything after it in the list. If not found, abort, reset the list and start again with getMailboxMessageList.
    4. Search for each of the messageIds in the removed list and remove them from the client’s list (without leaving a gap – this is a splice operation). If any can’t be found, keep processing but after finishing, null out anything after the first gap in the list. e.g. referring to the example sparse list above, ‘m1u8’ would be removed as it’s after the first gap.
    5. Iterate through the added list in order, inserting the messageIds at the positions indicated (again this is a splice operation and will shift everything else further along).
    6. Set the list length to that given in the total property.

    Note, adding or removing an item to/from the list shifts the position of everything around it.

    e.g. adding ‘m2’ in position 2: [ 'm1', 'm3' ] -> [ 'm1', 'm2', 'm3' ]

    e.g. removing ‘m2’: [ 'm1', 'm2', 'm3' ] -> [ 'm1', 'm3' ]

  3. messageUpdates: The removed array has 0 length (so, although a message was removed from the Inbox message list, it has not been deleted: it is simply no longer the first message in the thread in the given sort order). The changed array has three message ids: the first two we don’t have in memory, so we can ignore, but the last one we have in our cache so we should mark is as needing an update (i.e. the flags might be out of date).
  4. messages: Because we specified a fetchMessages argument to getMessageUpdates, this response contains the requested data for the messages that have been modified or added. The first two are new messages, and we can add this data to our header cache information so we have the info we need to display the mailbox. The final message is one we already have in cache, but a property of it has now changed (in this case, it is no longer unread). We can update this data and clear the flag we set in step 3 indicating it needs refreshing (the reason we set the flag is so the messageUpdates response is correctly handled regardless of whether we specified fetchMessages or not.
  5. threadUpdates: There are two changed threads here. One existing thread has a new message in it, the other is a brand new thread to create in our cache.

After applying these changes, our cache is completely in sync with the server, even though we only have a partial data set cached, and we only made a single HTTP request.

Now let’s look at the more difficult cases, when errors occur.

Handling errors

The most common error will be a tooManyChanges error, when the number of changes exceeds the number we were prepared to accept. In this case, it would be more efficient to throw away much of our cache, or mark it as potentially dirty, and then just fetch the information we need right now (similar to the cold boot situation), rather than fetching a potentially large set of updates. Let’s look at the errors for each method.

getMessageListUpdates: If there are more changes than the maxChanges argument, or the server is unable to calculate updates from the state requested, you will receive an appropriate error back. In these cases, you simply have to throw away the current message list cache and request the section you are interested in with a standard getMessageList call.

getMessageUpdates: If there are too many changes, you will get an error back. In this case, there are two strategies you could adopt. The first is simple: mark each message you have in cache as needing an update, and fetch these when the message is next required by the user (note, since only the flags and mailboxes it belongs to are mutable, the data you need to fetch can be reduced). As an extra possible optimisation, you could try another getMessageUpdates call first, with a higher maxChanges, but with fetchMessages set to null. If it succeeds, this will give you back an exact list of the message ids for messages with changes, so you can mark just those as needing a refresh rather than every message in your cache. If there are still too many changes, you will have to fall back to refetching all the flags and labels.

getThreadUpdates: Just like with getMessageUpdates, if there are too many changes you can either flush your cache of threads, or try with a higher maxChanges but fetchThreads: false.

In each of the error cases, a single further HTTP request should be sufficient to get the necessary data to update the client to the correct state.

Handling message list without delta updates

The response to getMessageList includes a property called canCalculateUpdates, which lets you know whether the server supports a call to getMessageListUpdates for the given message list (servers may not support it at all, or may only support it when there is no search involved etc.). In this case, we have to fetch the bit of the message list currently on display, and just throw away all of our cached data for the message list. Note, however, we don’t have to throw away all the message data, we just have to fetch the message ids, so it’s still not too inefficient.

Performing actions

Modifying the state is the most complex operation to get right, as changes to messages may change counts in mailboxes, or thread membership, and the client may not have sufficient information to work out all the effects; it must apply the change then get the rest of the updates from the server.

Selecting all

If you select all, you need to fetch the complete message list for the mailbox currently displayed.

When applying actions to threads, you will often wish to apply the same action to every message in the thread in the same mailbox as the one explicitly selected, or even to all the messages in the thread, regardless of mailbox. The most efficient way to do the former is to fetch the same message list, but with collapseConversations == false. You can then easily build a map of threadId to messages ids for messages in the list. If you need to find all messages in the thread, regardless of message list, you will need to fetch the Thread object for every thread in the message list (but you don’t need to fetch any message details). You can do this in a single getMessageList call, with fetchThreads: true, but fetchMessages: null, although if it’s a long message list you may wish to break this up into a few calls to avoid a single large request blocking the UI for too long.

Moving a message

Moving a message from the Inbox to the Trash is simple:

[ "setMessages", {
    update: {
        "fm1u254": {
            mailboxes: [ "mailbox3" ]
        }
    }
}, "call1" ]

We can then do a resync as in section 4 to update the client cache. This is easy, but means there’s a noticeable delay performing every action, and we cannot operate in an offline mode and resync later. We can improve on this, although it makes it considerably more complicated. We can apply the effects we think the changes will have instantly then compare this with the results we get back from the server.

  1. Mailbox counts.

    For each message moved: - Decrement the totalMessages count for the source mailbox. - Increment the totalMessages count for the destination mailbox. - If unread, decrement the unreadMessages count for the source mailbox. - If unread, increment the unreadMessages count for the destination mailbox.

    If there are no other messages in the thread in the source mailbox: - Decrement the totalThreads count for the source mailbox. - If any of the messages in the thread are unread, decrement the unreadThreads count for the source mailbox.

    If there are no other messages in the thread already in the destination mailbox: - Increment the totalThreads count for the destination mailbox. - If any of the messages in the thread are unread, increment the unreadThreads count for the destination mailbox.

    There are slight added complexities if moving a message into or out of the Trash; refer to the spec for full details.

  2. Message list. Splice each selected message being moved from the current message list (from which it was selected by the user). You may also try to insert them at the correct position in the message list of the destination mailbox:

    From the thread object, you know if there are any other messages in the thread already in the mailbox. As the message list in the thread object is sorted by date, as long as the mailbox list is also sorted by date, you can now work out if the message is a new exemplar in the message list or not. If it is, binary search the list by date to find where to insert it. If you don’t have message headers for all the message list loaded, you may not be able to determine the correct spot. In this case, it should be inserted anyway so the user can access it; the mistake will be corrected when the next sync with the server occurs.

  3. Thread. No changes to make.

  4. Message. Update the mailboxIds property on the message object.

When you next do resync, whether that’s a second or several days later, apply the changes and fetch the delta update (as per section 4) in a single request, and when the response is received, undo the preemptive changes made above and apply the real changes. Hopefully the end result will be the same, and there will be no noticeable difference to the user.

Keeping a full copy of mail in sync

JMAP can also be used to efficiently download and keep in sync the entire set of a user’s mail. To do this, you would firstly get the full set of mailboxes, then page in the full list of message ids and download the actual messages. From this point on, you would only need to get the delta updates. Presuming you also want to optimise for fetching new mail first, you could do something like when you want to get the latest updates:

[
    [ "getMailboxUpdates", {
        sinceState: "m123456789",
        fetchRecords: true,
        fetchRecordProperties: null
    }, "call1" ],
    [ "getMessageUpdates", {
        sinceState: "m815034"
        maxChanges: 50,
        fetchRecords: true,
        fetchRecordProperties: [
            "threadId",
            "mailboxIds",
            "isUnread",
            "isFlagged",
            "isAnswered",
            "isDraft",
            "hasAttachment",
            "labels",
            "from",
            "to",
            "subject",
            "date",
            "preview"
        ]
    }, "call2" ],
    [ "getMessageList", {
        filter: {
            inMailboxes: [ "${inboxId}" ]
        },
        sort: [ "date desc", "id desc" ]
        collapseThreads: false,
        position: 0,
        limit: 100
    }, "call3" ]
]

The first two calls get the delta updates to mailboxes and messages (if the client has a copy of all messages locally, it has no need to use the server to get threads as it already has all the information). In the common case where there are fewer than 50 message changes since last time, this will bring the client fully up to date (barring downloading new message bodies, attachments and other details, which it can easily schedule at its leisure).

However, if there have been a large number of changes since last time, the client is not yet fully up to date. It can continue to call getMessageUpdates to fetch more changes until it reaches the current state. Simultaneously, it can use the response to the getMessageList call in the first request to see if there are any new message ids at the top of the Inbox. These can be downloaded first so the user has immediate access to new messages (by the end of the second round trip) while the other changes then continue to sync across afterwards.