cometchat-react-push — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited cometchat-react-push (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
Web push for CometChat chat — new-message notifications when the user's tab is backgrounded or closed.
The web DOES have CometChat-native push. The JS Chat SDK ships a first-class web-push registration API: CometChatNotifications.registerPushToken(token, PushPlatforms.FCM_WEB). Paired with a Firebase Cloud Messaging (FCM) provider configured in the CometChat dashboard, CometChat's own backend delivers the push on every new message — no self-hosted push server, no web-push lib, no VAPID server of your own, and no message-sent webhook for the basic case. This is the recommended path (§1).
A self-hosted Web Push path (your own VAPID keys + push server + CometChat webhook) is documented as an advanced fallback (§7) for projects that can't or won't use Firebase. Don't reach for it unless you have to.
Not the same as the calls Web Push. Calls Web Push tries to ring the device through a closed tab (best-effort, browser-dependent). Chat push notifies on new messages — different payload + UX. Many apps need both.
Read these other skills first:
cometchat-core — provider pattern, login ordercometchat-{react,nextjs,react-router,astro}-patterns — framework-specific Service Worker registrationcometchat-react-calls/references/voip-and-web-push.md — calls-specific Web Push (overlap with this; both can coexist)cometchat-production — server-minted auth tokensGround truth (verified against the JS Chat SDK source): Official docs: https://www.cometchat.com/docs/notifications/overview · Docs MCP: claude mcp add --transport http cometchat-docs https://www.cometchat.com/docs/mcp (or fetch the URL directly without MCP).
CometChatNotifications.registerPushToken(pushToken: string, platform: PushPlatforms, providerId?: string): Promise<string> — chat-sdk-javascript/src/Notifications/CometChatNotifications.ts:616CometChatNotifications.unregisterPushToken(): Promise<string> — chat-sdk-javascript/src/Notifications/CometChatNotifications.ts:649enum PushPlatforms { FCM_WEB = 'fcm_web' } — chat-sdk-javascript/src/Notifications/constants/CometChatNotificationsConstants.ts:46-47@cometchat/chat-sdk-javascript ^4.1.10, which has this API.PNPlatform and NO PushNotificationOptions symbol — the real names are PushPlatforms and CometChatNotifications.CometChat delivers web push through Firebase Cloud Messaging for Web. You register the FCM device token with CometChat via the Chat SDK; CometChat's backend then sends pushes on new messages through your dashboard-configured FCM provider. No push server of yours.
Browser (your React app)
├── firebase/messaging — getToken() → FCM web token
├── firebase-messaging-sw.js — receives background pushes (onBackgroundMessage)
└── CometChatNotifications.registerPushToken(token, FCM_WEB) — hands token to CometChat
CometChat backend
└── FCM provider (dashboard) → sends push to the token on every new messageapiKey, projectId, messagingSenderId, appId, …).getToken({ vapidKey }). It is NOT a self-hosted VAPID server; you don't run any server for it.npm install firebase// cometchat/firebase.ts
import { initializeApp } from "firebase/app";
export const firebaseApp = initializeApp({
apiKey: import.meta.env.VITE_FIREBASE_API_KEY, // adjust prefix per framework
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
messagingSenderId: import.meta.env.VITE_FIREBASE_SENDER_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID,
});
export const FIREBASE_WEB_PUSH_CERT_KEY = import.meta.env.VITE_FIREBASE_VAPID_KEY; // Web Push certificate keypublic/firebase-messaging-sw.jsThe file MUST live at the origin root (/firebase-messaging-sw.js) — getToken looks for it by default, or pass an explicit serviceWorkerRegistration.
Coexistence check first (P0-10). Ifdetectreportedcoexistence.existing_firebase: trueor listed an existing service worker, the project already runs Firebase and/or a SW. Do NOT blindly overwrite: (a) reuse the project's existing Firebase config/app instead of a secondinitializeApp— duplicate apps fight over the FCM token; (b) if a service worker already exists at the origin root (e.g. a PWA/Workboxsw.js), merge the messaging handlers (importScripts+firebase.initializeApp+onBackgroundMessage) into it rather than replacing it, or register CometChat's worker at a distinct scope and pass it asserviceWorkerRegistrationtogetToken. Overwriting the app's worker silently kills its offline/caching logic.
// public/firebase-messaging-sw.js
importScripts("https://www.gstatic.com/firebasejs/10.12.0/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/10.12.0/firebase-messaging-compat.js");
firebase.initializeApp({
apiKey: "…",
projectId: "…",
messagingSenderId: "…",
appId: "…",
});
const messaging = firebase.messaging();
// Background pushes (tab closed / not focused)
messaging.onBackgroundMessage((payload) => {
const { title, body } = payload.notification ?? {};
self.registration.showNotification(title ?? "New message", {
body: body ?? "",
icon: "/icons/chat.png",
tag: payload.data?.conversationId ? `chat-${payload.data.conversationId}` : undefined,
data: payload.data ?? {},
});
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const data = event.notification.data ?? {};
const targetUrl = data.receiverType === "group"
? `/messages?group=${data.conversationId}`
: `/messages?user=${data.senderUid ?? data.conversationId}`;
event.waitUntil(
clients.matchAll({ type: "window", includeUncontrolled: true }).then((wins) => {
for (const w of wins) {
if (w.url.includes(self.registration.scope)) {
w.focus();
w.postMessage({ type: "open_conversation", ...data });
return;
}
}
return clients.openWindow(targetUrl);
}),
);
});// cometchat/registerFcmWebPush.ts
import { getMessaging, getToken, onMessage } from "firebase/messaging";
import { CometChatNotifications } from "@cometchat/chat-sdk-javascript";
import { firebaseApp, FIREBASE_WEB_PUSH_CERT_KEY } from "./firebase";
export async function registerFcmWebPush(): Promise<void> {
if (!("serviceWorker" in navigator) || !("Notification" in window)) return;
// Permission MUST be requested in response to a user gesture (see §2 rule).
const permission = await Notification.requestPermission();
if (permission !== "granted") return;
// Register the FCM service worker.
const swReg = await navigator.serviceWorker.register("/firebase-messaging-sw.js");
const messaging = getMessaging(firebaseApp);
const fcmToken = await getToken(messaging, {
vapidKey: FIREBASE_WEB_PUSH_CERT_KEY, // Firebase Web Push certificate — NOT a self-hosted VAPID server
serviceWorkerRegistration: swReg,
});
if (!fcmToken) return;
// Hand the token to CometChat. Its backend now delivers pushes on new messages.
await CometChatNotifications.registerPushToken(
fcmToken,
CometChatNotifications.PushPlatforms.FCM_WEB,
/* providerId */ undefined, // omit for "default", or pass your dashboard FCM provider id
);
// Foreground messages (tab focused) don't fire the SW — show them yourself if desired.
onMessage(messaging, (payload) => {
if (document.visibilityState === "visible") {
// e.g. toast / in-app banner instead of an OS notification
window.dispatchEvent(new CustomEvent("cometchat:foreground-push", { detail: payload }));
}
});
}Call it from your provider AFTER CometChatUIKit.login(...) resolves:
// CometChatProvider.tsx
useEffect(() => {
if (!user) return;
registerFcmWebPush().catch((err) => {
console.warn("FCM web push registration failed:", err); // never block chat — push is opt-in
});
}, [user]);In the CometChat dashboard: Notifications → Push Notifications → Add FCM provider. Upload the Firebase service-account JSON (or legacy server key) for the same Firebase project. CometChat now sends new-message pushes to every registered FCM_WEB token — no webhook of yours.
import { CometChatNotifications } from "@cometchat/chat-sdk-javascript";
// Call BEFORE CometChat.logout(), while the auth token is still valid.
await CometChatNotifications.unregisterPushToken(); // CometChatNotifications.ts:649Hard rule — register only AFTER login.registerPushTokenoperates on the current logged-in user's auth token. Calling it beforeCometChatUIKit.login(...)resolves will fail or attach the token to nobody. Wire it inside the auth-state effect, never on page load.
Hard rule — `firebase-messaging-sw.js` must be at the origin root. Place it inpublic/so it serves from/firebase-messaging-sw.js. In Next.js it cannot live underapp/. See §8 for per-framework placement.
Hard rule — iOS still needs PWA install. FCM web push on iOS 16.4+ only works for a Home-Screen-installed PWA, exactly like the fallback path. See §9.
Chrome / Firefox / Safari all require Notification.requestPermission() to run in response to a user gesture (a click). Calling it from a top-level useEffect on page load is rejected. Best pattern: an "Enable notifications" button the user clicks once, which then calls registerFcmWebPush() (or the fallback registerWebPushForChat()).
Use this path only if you cannot or will not use Firebase (§1). It is more work — you run your own VAPID keys, a push server, and a CometChat message-sent webhook. CometChat's backend does NOT deliver the push for you on this path; your server does. For most apps, prefer §1.
Browser (your React app)
├── Service Worker — long-lived; receives push events
├── Push subscription — issued by browser, sent to your server
└── Notification UI — fired by SW on push event
CometChat backend
└── Webhook → your push server when a message is sent
Your push server (Node, Cloudflare Worker, Lambda, etc.)
├── Stores push subscriptions per UID
├── Receives CometChat webhook
└── Sends push payload via web-push lib → browserThree pieces, all yours: client SW, push server, webhook integration.
VAPID = Voluntary Application Server Identification — proves to the browser that the push originated from an authorized server.
npx web-push generate-vapid-keysOutput:
=======================================
Public Key: BLBz-...
Private Key: 9tT...
=======================================Public key → client (env var). Private key → push server only (never ship to client).
#### public/sw.js (Vite / CRA / React Router) or app/sw.js (Next.js / Astro)
// Fired when a push payload arrives
self.addEventListener("push", (event) => {
if (!event.data) return;
let payload;
try {
payload = event.data.json();
} catch {
return;
}
if (payload.type !== "new_message") return;
event.waitUntil(
self.registration.showNotification(payload.senderName, {
body: payload.preview, // truncated message
icon: payload.senderAvatar ?? "/icons/chat.png",
tag: `chat-${payload.conversationId}`, // dedupe — replace prior unread for same conversation
badge: "/icons/badge.png",
data: {
conversationId: payload.conversationId,
senderUid: payload.senderUid,
receiverType: payload.receiverType, // "user" or "group"
},
}),
);
});
// Fired when user clicks the notification
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const data = event.notification.data;
const targetUrl = data.receiverType === "group"
? `/messages?group=${data.conversationId}`
: `/messages?user=${data.senderUid}`;
event.waitUntil(
clients.matchAll({ type: "window", includeUncontrolled: true }).then((wins) => {
// Focus existing tab if open
for (const w of wins) {
if (w.url.includes(self.registration.scope)) {
w.focus();
w.postMessage({ type: "open_conversation", ...data });
return;
}
}
// Otherwise open a new tab
return clients.openWindow(targetUrl);
}),
);
});
// Optional: fired when a notification is dismissed without click
self.addEventListener("notificationclose", (event) => {
// Send a "dismissed" beacon to your server if you track this
});// cometchat/registerWebPush.ts
const VAPID_PUBLIC = import.meta.env.VITE_VAPID_PUBLIC_KEY; // adjust prefix per framework
export async function registerWebPushForChat(uid: string): Promise<void> {
if (!("serviceWorker" in navigator) || !("PushManager" in window)) return;
// Register the SW (one-time per origin)
const reg = await navigator.serviceWorker.register("/sw.js");
// Wait for SW activation
await navigator.serviceWorker.ready;
// Ask permission — must be in response to a user gesture (click)
const permission = await Notification.requestPermission();
if (permission !== "granted") return;
// Get or create subscription
let subscription = await reg.pushManager.getSubscription();
if (!subscription) {
subscription = await reg.pushManager.subscribe({
userVisibleOnly: true, // required by Chrome
applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC),
});
}
// Send subscription to YOUR push server, keyed by uid
await fetch("/api/push/subscribe", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ uid, subscription }),
});
}
function urlBase64ToUint8Array(base64: string): Uint8Array {
const padding = "=".repeat((4 - (base64.length % 4)) % 4);
const raw = atob((base64 + padding).replace(/-/g, "+").replace(/_/g, "/"));
return Uint8Array.from(raw, (c) => c.charCodeAt(0));
}Call this from your CometChatProvider AFTER login resolves:
// CometChatProvider.tsx
useEffect(() => {
if (!user) return;
registerWebPushForChat(user.uid).catch((err) => {
// surface to UI but don't block chat — push is opt-in
console.warn("Web Push registration failed:", err);
});
}, [user]);The permission-prompt rule (§2) applies here too — request permission from a user gesture, not page load.
// CometChatProvider.tsx
useEffect(() => {
if (!("serviceWorker" in navigator)) return;
const handler = (event: MessageEvent) => {
if (event.data?.type === "open_conversation") {
navigate(`/messages?${event.data.receiverType}=${event.data.conversationId}`);
}
};
navigator.serviceWorker.addEventListener("message", handler);
return () => navigator.serviceWorker.removeEventListener("message", handler);
}, [navigate]);When the SW posts open_conversation, the React app navigates to the right thread.
Your push server runs on Node.js / Cloudflare Worker / Lambda / Vercel Functions. The shape:
// server/push.ts
import express from "express";
import webpush from "web-push";
import { z } from "zod";
webpush.setVapidDetails(
"mailto:[email protected]",
process.env.VAPID_PUBLIC!,
process.env.VAPID_PRIVATE!,
);
const app = express();
app.use(express.json());
// 1. Client registers a subscription
app.post("/api/push/subscribe", async (req, res) => {
const { uid, subscription } = req.body; // validate with zod in production
await db.savePushSubscription(uid, subscription);
res.status(204).send();
});
// 2. CometChat fires a webhook when a message is sent
app.post("/webhook/cometchat/message-sent", async (req, res) => {
const { receiver, sender, data } = req.body.data;
// Don't notify the sender — they sent the message
const subs = await db.getPushSubscriptions(receiver);
if (!subs.length) return res.status(204).send();
const payload = JSON.stringify({
type: "new_message",
conversationId: receiver,
senderUid: sender.uid,
senderName: sender.name,
senderAvatar: sender.avatar,
preview: truncate(data.text, 80),
receiverType: data.entityType, // "user" or "group"
});
// Send to all of receiver's subscriptions in parallel; clean up dead ones
await Promise.allSettled(
subs.map(async (sub) => {
try {
await webpush.sendNotification(sub, payload);
} catch (err: unknown) {
if ((err as { statusCode?: number }).statusCode === 410) {
await db.removePushSubscription(receiver, sub); // browser unsubscribed
}
}
}),
);
res.status(204).send();
});
function truncate(s: string, n: number) {
return s.length <= n ? s : s.slice(0, n - 1) + "…";
}
app.listen(3000);The skill writes a starter version of this server file (server/push.example.ts) with a README pointing at env vars; the user owns the actual deployment.
In the CometChat dashboard:
https://your-push-server.example.com/webhook/cometchat/message-sentCOMETCHAT_WEBHOOK_SECRET).The webhook fires for EVERY message — your server filters out the sender, dedupes per conversation, and respects user notification preferences.
import crypto from "crypto";
app.use("/webhook/cometchat", (req, res, next) => {
const signature = req.header("x-cometchat-signature");
const expected = crypto
.createHmac("sha256", process.env.COMETCHAT_WEBHOOK_SECRET!)
.update(JSON.stringify(req.body))
.digest("hex");
if (signature !== expected) return res.status(401).send({ error: "invalid signature" });
next();
});Without this, anyone with your endpoint URL can flood your users with fake notifications.
| Browser | Web Push | Notification while closed | Notes |
|---|---|---|---|
| Chrome desktop | ✓ | ✓ if Chrome process alive | Service Worker terminates after ~30s idle |
| Edge desktop | ✓ | ✓ | Same as Chrome |
| Firefox desktop | ✓ | ✓ | Slightly more SW survival |
| Safari 16+ desktop | ✓ | ✓ if Safari running | macOS 13+ |
| Safari 16.4+ iOS | ✓ | Only when added to Home Screen as PWA | Critical caveat |
| Chrome mobile | ✓ | ✓ | Aggressive throttling on Android |
| Edge mobile | ✓ | ✓ | Same as Chrome mobile |
iOS PWA-only requirement (applies to FCM_WEB too): iOS 16.4+ supports web push, but ONLY for sites added to the Home Screen as a PWA. Safari-the-browser-app does NOT receive push. This is platform-level and applies equally to the FCM_WEB path (§1) and the self-hosted path (§3). To unlock iOS web push:
manifest.json (PWA manifest)This is a real production constraint. The skill detects whether the project ships a manifest.json and warns if not.
The FCM service worker (firebase-messaging-sw.js, §1) and the self-hosted SW (sw.js, §3) follow the same placement rules.
public/firebase-messaging-sw.js (or public/sw.js) is served from the origin root. register("/firebase-messaging-sw.js") works directly.
Service Workers + Next.js have a known gotcha: the SW can't be inside app/ because Next handles those routes. Place it in public/ and serve from the origin root. Register from a "use client" component that runs after hydration.
Same — public/.
public/ works. If using SSR (loaders), the SW registration code must be guarded by typeof window !== "undefined".
Place the SW at public/. Register from a client:only="react" island.
The framework-specific patterns skills cover the SSR guards in detail.
Service Workers + Push API + FCM all require HTTPS (or localhost for dev). The skill detects the dev server protocol and warns:
⚠️ Web push requires HTTPS or localhost. Your dev server is running on http://192.168.x.x.
Push subscriptions will fail. Either:
- Use http://localhost (Chrome/Firefox/Safari all allow Push API on localhost), or
- Set up HTTPS dev (mkcert, ngrok, or Vite's --https flag)For production, the Vercel / Netlify / Cloudflare default deploys are HTTPS — no extra work.
CometChatNotifications.registerPushToken(token, PushPlatforms.FCM_WEB) + a dashboard FCM provider (§1). Don't hand-roll a push server unless you've ruled out Firebase.PNPlatform, no PushNotificationOptions. The real names are CometChatNotifications and PushPlatforms (only member: FCM_WEB).CometChatUIKit.login(...) resolves.clients.matchAll() (self-hosted) or document.visibilityState in onMessage (FCM) and skip if the user already has the chat focused.CometChatNotifications.unregisterPushToken() (§1.6). Self-hosted path: subscription.unsubscribe() + DELETE the server record (§8). Otherwise the previous user keeps getting the new user's messages.(FCM_WEB logout uses CometChatNotifications.unregisterPushToken() — see §1.6. This is the self-hosted equivalent.)
async function unsubscribeWebPush(uid: string): Promise<void> {
const reg = await navigator.serviceWorker.getRegistration();
if (!reg) return;
const subscription = await reg.pushManager.getSubscription();
if (!subscription) return;
await fetch("/api/push/unsubscribe", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ uid, subscription }),
});
await subscription.unsubscribe();
}Call this from your logout flow before CometChat.logout().
Recommended FCM_WEB path (§1):
firebase installed; firebaseApp initialized with the web SDK configgetToken({ vapidKey })public/firebase-messaging-sw.js at origin root, handles onBackgroundMessage + notificationclickNotification.requestPermission() triggered from a user gesture, not page load (§2)CometChatNotifications.registerPushToken(token, CometChatNotifications.PushPlatforms.FCM_WEB) called AFTER CometChatUIKit.login(...) resolvesonMessage does NOT pop an OS notification when the tab is focusedCometChatNotifications.unregisterPushToken() before CometChat.logout()PNPlatform / PushNotificationOptions) anywhereCommon to both paths:
manifest.json shipped if iOS users are expected (PWA caveat, §4)tag fieldSelf-hosted fallback path (§3) only:
public/sw.js exists and listens for push + notificationclick eventsMessage sent eventssubscription.unsubscribe() and deletes server recordcometchat-react-calls/references/voip-and-web-push.md — Web Push for incoming calls (overlap; both can coexist on the same SW)cometchat-{react,nextjs,react-router,astro}-patterns — framework-specific SSR handlingcometchat-production — auth tokens, securitycometchat-troubleshooting — web push debugging (chrome://serviceworker-internals, Firefox about:debugging)~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.