Generate a production-grade React MQTT context for CloudSignal real-time notifications over WebSocket. Supports Clerk, Supabase, Auth0, Firebase, and custom OIDC auth providers. Use when implementing real-time notifications, live updates, job progress tracking, or WebSocket messaging with CloudSignal.
Generate a production-grade React context provider for real-time messaging over CloudSignal's MQTT broker using WebSocket. The generated code handles the full connection lifecycle: authentication via external auth providers, reconnection, proactive token refresh, message routing, and cleanup.
mqtt-context.tsx — React context provider with:
cloudsignal.d.ts — TypeScript declarations for @cloudsignal/mqtt-client (the npm package does not ship types)
Ask the user for these inputs (use defaults if not provided):
| Input | Example | Default |
|-------|---------|---------|
| Auth provider | Clerk, Supabase, Auth0, Firebase | Ask (required) |
| Organization ID | org_k7xm4pqr2n5t | Ask (required) |
| Topic namespace | myapp | App name from package.json |
| Message types needed | notifications, jobs, transactions | All three |
| Target directory | src/lib/ or src/contexts/ | src/lib/ |
Read references/mqtt-context.tsx in this skill's directory. This is the canonical reference — a production-tested context provider extracted from a live SaaS. Use it as the base for all generated code.
The reference contains two placeholder functions that MUST be replaced based on the user's auth provider:
useCurrentUser() — must return { id: string } | null:
| Provider | Implementation |
|----------|---------------|
| Clerk | import { useUser } from "@clerk/nextjs"; const { user } = useUser(); return user ? { id: user.id } : null; |
| Supabase | import { useSession } from "@supabase/auth-helpers-react"; const session = useSession(); return session ? { id: session.user.id } : null; |
| Auth0 | import { useAuth0 } from "@auth0/auth0-react"; const { user, isAuthenticated } = useAuth0(); return isAuthenticated && user ? { id: user.sub! } : null; |
| Firebase | import { useAuthState } from "react-firebase-hooks/auth"; import { auth } from "@/lib/firebase"; const [user] = useAuthState(auth); return user ? { id: user.uid } : null; |
useGetToken() — must return () => Promise<string | null>:
| Provider | Implementation |
|----------|---------------|
| Clerk | import { useAuth } from "@clerk/nextjs"; const { getToken } = useAuth(); return getToken; |
| Supabase | import { useSupabaseClient } from "@supabase/auth-helpers-react"; const supabase = useSupabaseClient(); return async () => { const { data } = await supabase.auth.getSession(); return data.session?.access_token ?? null; }; |
| Auth0 | import { useAuth0 } from "@auth0/auth0-react"; const { getAccessTokenSilently } = useAuth0(); return async () => { try { return await getAccessTokenSilently(); } catch { return null; } }; |
| Firebase | import { auth } from "@/lib/firebase"; return async () => auth.currentUser?.getIdToken() ?? null; |
Replace the reference message interfaces with the user's domain. Keep the structure (interface + handler type + registry pattern) but adapt field names and types.
Default message types from reference: ProgressMessage, StatusMessage, TransactionMessage, NotificationMessage.
const CLOUDSIGNAL_ORG_ID = process.env.NEXT_PUBLIC_CLOUDSIGNAL_ORG_ID;
const CLOUDSIGNAL_HOST = process.env.NEXT_PUBLIC_CLOUDSIGNAL_HOST || "wss://connect.cloudsignal.app:18885/";
const TOPIC_ROOT = "{user's namespace}";
Read references/cloudsignal.d.ts and write it to the user's types/ directory (or wherever their project keeps type declarations). This file is required because @cloudsignal/mqtt-client does not ship .d.ts files.
Combine the adapted auth hooks, customized message types, and the full provider logic from the reference into the final mqtt-context.tsx. Preserve ALL of these production patterns:
connectRef updated every render, used by timersauthErrorCountRef with MAX_AUTH_ERRORS = 3scheduleTokenRefresh() at 50 minutesconnectingRef + mountedRef prevent double connectionstopic === \${prefix}/notifications`not.includes()`JSON.parse in message handlervisibilitychange event listenerProvide a usage example showing:
app/providers.tsx (nested inside the auth provider)useMQTT() hook.env.local)Read references/sdk-pitfalls.md for the full list. These MUST be handled in generated code:
onAuthError, don't rely on SDK reconnectexternalToken not idToken: Use externalToken in connectWithToken()cloudsignal.d.tsconnectingRef + mountedRef guards/v2/tokens/exchangerequire() breaks mqtt.connect(): Always use ESM import, never require()Tell the user to add these to .env.local:
NEXT_PUBLIC_CLOUDSIGNAL_ORG_ID=org_xxxxxxxxxxxxx # From CloudSignal dashboard
NEXT_PUBLIC_CLOUDSIGNAL_HOST=wss://connect.cloudsignal.app:18885/ # Optional, this is default
Remind the user they need to configure their CloudSignal organization:
org_xxx ID for the environment variableThe user needs to install the CloudSignal MQTT client:
npm install @cloudsignal/mqtt-client
npx skills add cloudsignal/cloudsignal-websocket下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer