Push in practice
The push guide explains the three ways a JMAP server tells an app something changed. This one is about building with them: the settings that matter, and what to do after a gap.
EventSource
The session's eventSourceUrl is a template with three blanks:
| Blank | What to put |
|---|---|
types | Which kinds of data to hear about, such as Email,Mailbox, or * for everything |
closeafter | no to stay connected, or state to close after the first change |
ping | Seconds between pings when nothing happens. 30 to 60 stops proxies closing a quiet connection |
Browsers' built in EventSource can't send an Authorization header, so web apps often read the stream with fetch instead, as the push tester does.
Web Push
- Create a
PushSubscriptionwithPushSubscription/set, giving the push address, a device id and, ideally, encryption keys. - The server sends a verification code to that address first. Send it back with another
PushSubscription/setto switch the subscription on. - Subscriptions expire. Keep track of
expiresand renew before then.
With encryption keys, the push service in the middle can't read the alerts. Servers listing urn:ietf:params:jmap:webpush-vapid also sign them, so the push service knows where they came from.
After a gap
Alerts can be missed: phones sleep, connections drop and apps get closed. So never trust that you saw every alert. Each time you reconnect, call /changes for each type from the states you saved. That catches anything you missed, and if nothing changed it costs one small request.
Less work for the same result
- Changes often come in bursts. Wait a moment after an alert before fetching, so one fetch covers them all.
- Compare the state in the alert with the one you have. If they match, you're already up to date.
- Only listen for the types your app shows.
Words you don't know? See the glossary.