|
1 | | -# Fastmail EventSource Daemon |
| 1 | +# Fastmail EventSource daemon |
2 | 2 |
|
3 | | -A lightweight daemon that connects to Fastmail's JMAP EventSource API for real-time email notifications and forwards state changes to the main Inbox Zero application. |
| 3 | +This service receives JMAP EventSource (SSE) notifications and asks the web application to enqueue account synchronization. It needs no database access. The web application decrypts API tokens through its normal credential layer and returns them only to the authenticated internal configuration endpoint. |
4 | 4 |
|
5 | | -## Overview |
| 5 | +Run from the repository root with Node 24 and pnpm 11.19: |
6 | 6 |
|
7 | | -This daemon implements [JMAP EventSource](https://www.rfc-editor.org/rfc/rfc8620.html#section-7.3) (RFC 8620 section 7.3) to receive real-time push notifications from Fastmail when emails arrive or change state. |
8 | | - |
9 | | -### Architecture |
10 | | - |
11 | | -``` |
12 | | -┌─────────────────────────────────────────────────────────────────┐ |
13 | | -│ Self-Hosted Deployment │ |
14 | | -├─────────────────────────────────────────────────────────────────┤ |
15 | | -│ │ |
16 | | -│ ┌─────────────────────────────────────────────────────────────┐ │ |
17 | | -│ │ EventSource Daemon │ │ |
18 | | -│ │ (this service) │ │ |
19 | | -│ │ │ │ |
20 | | -│ │ For each Fastmail account: │ │ |
21 | | -│ │ 1. Connect to Fastmail EventSource │ │ |
22 | | -│ │ 2. On state change → POST to webhook │ │ |
23 | | -│ └──────────────────────────┬──────────────────────────────────┘ │ |
24 | | -│ │ │ |
25 | | -│ │ POST /api/fastmail/webhook │ |
26 | | -│ │ { emailAccountId, newState } │ |
27 | | -│ ▼ │ |
28 | | -│ ┌─────────────────────────────────────────────────────────────┐ │ |
29 | | -│ │ Next.js Application │ │ |
30 | | -│ │ │ │ |
31 | | -│ │ /api/fastmail/webhook ─────► pollFastmailAccount() │ │ |
32 | | -│ └─────────────────────────────────────────────────────────────┘ │ |
33 | | -└─────────────────────────────────────────────────────────────────┘ |
34 | | -``` |
35 | | - |
36 | | -## Requirements |
37 | | - |
38 | | -- Node.js 20+ |
39 | | -- Access to the same PostgreSQL database as the main Inbox Zero app |
40 | | -- Network access to the main Inbox Zero app webhook endpoint |
41 | | - |
42 | | -## Configuration |
43 | | - |
44 | | -Copy `.env.example` to `.env` and configure: |
45 | | - |
46 | | -```bash |
47 | | -# Required: Same database as main app |
48 | | -DATABASE_URL=postgresql://user:password@localhost:5432/inbox_zero |
49 | | - |
50 | | -# Required: Main app URL |
51 | | -MAIN_APP_URL=http://localhost:3000 |
52 | | - |
53 | | -# Required: Shared secret (must match FASTMAIL_WEBHOOK_SECRET in main app) |
54 | | -# Generate with: openssl rand -hex 32 |
55 | | -FASTMAIL_WEBHOOK_SECRET=your-secret-here |
56 | | - |
57 | | -# Required for OAuth token refresh |
58 | | -FASTMAIL_CLIENT_ID=your-fastmail-client-id |
59 | | -FASTMAIL_CLIENT_SECRET=your-fastmail-client-secret |
60 | | - |
61 | | -# Optional: Refresh interval (default: 5 minutes) |
62 | | -ACCOUNT_REFRESH_INTERVAL=300000 |
63 | | - |
64 | | -# Optional: Enable debug logging |
65 | | -DEBUG=false |
66 | | -``` |
67 | | - |
68 | | -## Running |
69 | | - |
70 | | -### Development |
71 | | - |
72 | | -```bash |
73 | | -pnpm install |
74 | | -pnpm dev |
75 | | -``` |
76 | | - |
77 | | -### Production |
78 | | - |
79 | | -```bash |
80 | | -pnpm install |
81 | | -pnpm start |
82 | | -``` |
83 | | - |
84 | | -### Docker |
85 | | - |
86 | | -```bash |
87 | | -docker build -t inbox-zero-fastmail-daemon . |
88 | | -docker run -e DATABASE_URL=... -e MAIN_APP_URL=... -e FASTMAIL_WEBHOOK_SECRET=... inbox-zero-fastmail-daemon |
89 | | -``` |
90 | | - |
91 | | -### Docker Compose |
92 | | - |
93 | | -Add to your `docker-compose.yml`: |
94 | | - |
95 | | -```yaml |
96 | | -services: |
97 | | - fastmail-daemon: |
98 | | - build: ./apps/fastmail-eventsource-daemon |
99 | | - environment: |
100 | | - - DATABASE_URL=${DATABASE_URL} |
101 | | - - MAIN_APP_URL=http://web:3000 |
102 | | - - FASTMAIL_WEBHOOK_SECRET=${FASTMAIL_WEBHOOK_SECRET} |
103 | | - - FASTMAIL_CLIENT_ID=${FASTMAIL_CLIENT_ID} |
104 | | - - FASTMAIL_CLIENT_SECRET=${FASTMAIL_CLIENT_SECRET} |
105 | | - depends_on: |
106 | | - - web |
107 | | - - postgres |
108 | | - restart: unless-stopped |
| 7 | +```sh |
| 8 | +pnpm --filter inbox-zero-fastmail-eventsource-daemon build |
| 9 | +pnpm --filter inbox-zero-fastmail-eventsource-daemon start |
109 | 10 | ``` |
110 | 11 |
|
111 | | -## How It Works |
112 | | -
|
113 | | -1. **Startup**: The daemon queries the database for all Fastmail accounts with: |
114 | | - - Valid access token |
115 | | - - At least one enabled automation rule |
116 | | - - Premium tier with AI access (or self-hosted bypass) |
117 | | -
|
118 | | -2. **Per Account**: For each account, it: |
119 | | - - Fetches the JMAP session to get the EventSource URL |
120 | | - - Opens a Server-Sent Events (SSE) connection to Fastmail |
121 | | - - Subscribes to Email state changes |
| 12 | +Set `MAIN_APP_URL`, `INTERNAL_API_KEY`, and `FASTMAIL_WEBHOOK_SECRET` as described in `.env.example`. Keep the internal API key private. Build the container from the repository root: |
122 | 13 |
|
123 | | -3. **On State Change**: When Fastmail sends a state change event: |
124 | | - - The daemon POSTs to `/api/fastmail/webhook` on the main app |
125 | | - - The main app triggers `pollFastmailAccount()` with `forceSync: true` |
126 | | - - Emails are processed through the rule engine |
127 | | - |
128 | | -4. **Periodic Refresh**: Every 5 minutes (configurable), the daemon: |
129 | | - - Re-queries the database for accounts |
130 | | - - Starts connections for new accounts |
131 | | - - Closes connections for removed/ineligible accounts |
132 | | - |
133 | | -## Error Handling |
134 | | - |
135 | | -- **Connection errors**: Exponential backoff reconnection (1s, 2s, 4s... up to 5 min) |
136 | | -- **Token expiration (401)**: Automatic token refresh using OAuth refresh token |
137 | | -- **Webhook failures**: Retry with exponential backoff |
138 | | -- **Database errors**: Logged and continued (existing connections maintained) |
139 | | - |
140 | | -## Future: PushSubscription |
141 | | - |
142 | | -When Fastmail adds support for JMAP PushSubscription (RFC 8620 section 7.2), |
143 | | -this daemon will become unnecessary. Fastmail will be able to POST directly |
144 | | -to the `/api/fastmail/webhook` endpoint, which is already designed to handle this. |
145 | | - |
146 | | -The webhook endpoint in the main app is compatible with both: |
147 | | -1. This EventSource daemon (current) |
148 | | -2. Future Fastmail PushSubscription webhooks |
149 | | - |
150 | | -## Logs |
151 | | - |
152 | | -The daemon logs to stdout/stderr. In production, capture logs with your container orchestrator or process manager. |
153 | | - |
154 | | -Example output: |
| 14 | +```sh |
| 15 | +docker build -f apps/fastmail-eventsource-daemon/Dockerfile -t inbox-zero-fastmail-daemon . |
155 | 16 | ``` |
156 | | -╔═══════════════════════════════════════════════════════════════╗ |
157 | | -║ Fastmail EventSource Daemon ║ |
158 | | -║ ║ |
159 | | -║ Connects to Fastmail for real-time email notifications ║ |
160 | | -║ and forwards state changes to the main app webhook. ║ |
161 | | -╚═══════════════════════════════════════════════════════════════╝ |
162 | 17 |
|
163 | | -Main App URL: http://localhost:3000 |
164 | | -Debug Mode: disabled |
| 18 | +The daemon refreshes accounts every minute, reconnects after disconnects or missing heartbeats, and requests recovery on every connection. Webhook delivery is bounded and retried; the independent five-minute cron poll recovers missed notifications even when the daemon is offline. The web application commits incoming message IDs and the JMAP cursor together before processing work. Failed items remain pending for retry. |
165 | 19 |
|
166 | | -[2024-01-15T10:30:00.000Z] [AccountManager] Starting account manager |
167 | | -[2024-01-15T10:30:00.100Z] [AccountManager] Found 3 eligible Fastmail accounts |
168 | | -[2024-01-15T10:30:00.200Z] [AccountManager] Adding connection for user@example.com |
169 | | -[2024-01-15T10:30:00.500Z] [AccountManager] Connected: abc123 |
170 | | -[Stats] Connections: 3/3 active |
171 | | -``` |
| 20 | +See [Fastmail self-hosting](../../docs/FASTMAIL_SELF_HOSTING.md) for the full deployment and release checks. |
0 commit comments