Skip to content

feat(realtime): service worker support and FCM fallback for device tokens - #12

Merged
3m1n3nc3 merged 1 commit into
mainfrom
feat/realtime-device-token
Oct 10, 2026
Merged

3m1n3nc3 merged 1 commit into
mainfrom
feat/realtime-device-token

Conversation

@3m1n3nc3

Copy link
Copy Markdown
Member

Summary

Makes RealtimeClient.deviceToken() usable for browser push in real apps.

  • Service worker option: deviceToken({ serviceWorker }) accepts a ServiceWorkerRegistration or a script URL. A URL is registered with the Firebase web config passed as the firebaseConfig query parameter, so a static worker file can initialize Firebase with no build step or secrets. Without the option, Firebase's default /firebase-messaging-sw.js lookup still applies.
  • Unsupported browsers: returns null where web push is unavailable (via isSupported()) instead of throwing.
  • FCM fallback: when the streaming transport can't issue tokens, deviceToken() uses the client's firebase config. This covers notifications streamed over Pusher or polling and pushed over FCM; the server's Broadcast.clientConfig() already includes that config when drivers.realtime.push is 'firebase'.
  • New helper: requestDeviceToken(firebaseConfig, options) issues a token without a realtime client.
  • Docs: the notifications guide has a "Registering a browser for push" section with a ready-to-use push-sw.js. It shows the notification, honors retractions, and opens the notification's link on click.

Test plan

  • New tests:
    • registers by URL, passing the config through the query string;
    • passes an existing registration through;
    • returns null where push is unsupported;
    • falls back to the firebase config;
    • returns null when nothing can issue a token.
  • pnpm lint, pnpm build, pnpm docs:build, realtime tsc --noEmit
  • vitest run: 576 passed, 5 skipped
  • Manual check in a browser against a real Firebase project

…kens

- deviceToken({ serviceWorker }) accepts a ServiceWorkerRegistration or a
  script URL; a URL is registered with the Firebase web config passed as the
  firebaseConfig query parameter, so a static worker can initialize Firebase.
- Returns null where web push is unsupported instead of throwing.
- RealtimeClient.deviceToken() falls back to the firebase config when the
  streaming transport cannot issue tokens (push over FCM, stream over Pusher
  or polling). requestDeviceToken() issues a token without a client.
- Document browser push registration with a ready-to-use service worker.
@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/realtime/src/push.ts 93.10% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@3m1n3nc3
3m1n3nc3 merged commit 98a8450 into main Oct 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant