Reacting to new email instantly Intermediate
Instead of checking mail on a timer, you can be notified the moment mail arrives: Gmail push notifications or Microsoft Graph change notifications.
What you'll learn
- Polling vs push
- How Gmail push works
- How Outlook notifications work
Before you start
- The Grok Bot app, installed and signed in. New? See where to download it and installing and signing in.
- Comfort with the ideas in Webhooks.
1. Why push?
A routine that checks every 15 minutes can be up to 15 minutes late. Push means the mail service tells your code right away.
2. Gmail: watch + Pub/Sub
- Create a Google Cloud Pub/Sub topic (a message channel).
- Give Gmail permission to publish to it.
- Call Gmail's
watchon the mailbox. - Your code receives a notice, then fetches the new messages.
- Renew the watch regularly; Google says to call watch at least every 7 days.
3. Outlook: Graph change notifications
- Create a subscription on the user's messages with your webhook URL.
- Answer Microsoft's validation request.
- Receive notifications, fetch the message, act.
- Renew before the subscription expires.
4. Keep it simple
For most people, a routine every 15-30 minutes is plenty. Push is for when minutes matter.
Ideas to try
- Support desk.
Explain how I'd get notified instantly when a customer emails support@.
- Urgent alerts.
Text me right away when an email from my bank arrives (plan only).
- Compare.
Is push worth it for me, or is a 15-minute routine enough?
- Build.
Draft code to receive Gmail Pub/Sub notifications on a Cloudflare Worker.
Tips
- Always fetch the message yourself; notices carry little data.
- Handle duplicates.
- Plan for renewals or it silently stops.
Facts about the Grok Bot app come from Cursor's official Grok Bot documentation. Docs: Gmail push, Graph change notifications. Menus and services change; if something looks different, check the official docs or ask your bot.