agentleFS
charanjit-singhcharanjit-singh/lovable-skills/skills/realtime-and-subscriptions/SKILL.md

realtime-and-subscriptions

Skill10 starsChanged 6 months agoMITView on GitHub

Use when adding Supabase Realtime, live updates, chat, notifications, or postgres_changes subscriptions. Not for one-time fetches (use data hooks) or polling unless Realtime is unavailable.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use Supabase Realtime for live data.

    line 11
  2. Very specific:

    Prefer narrow filters (filter: \room_id=eq.${id}\``) over subscribing to whole tables.

    line 44
  3. Very specific:

    Use channel.on("presence", ...) or broadcast only when product requires it.

    line 50

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Realtime and subscriptions

Use Supabase Realtime for live data; pair with existing query cache (SWR/React Query) for initial load.

Setup

  1. Enable Realtime on the table in Supabase (publication / replica identity as required).
  2. RLS must allow the subscriber to SELECT rows they receive.

Subscription pattern

useEffect(() => {
  if (!channelId) return;

  const channel = supabase
    .channel(`room:${channelId}`)
    .on(
      "postgres_changes",
      { event: "*", schema: "public", table: "messages", filter: `room_id=eq.${channelId}` },
      (payload) => {
        // merge into local state or call mutate() on SWR/React Query key
      }
    )
    .subscribe();

  return () => {
    supabase.removeChannel(channel);
  };
}, [channelId]);

Rules

  • Always unsubscribe on unmount (removeChannel).
  • Prefer narrow filters (filter: \room_id=eq.${id}``) over subscribing to whole tables.
  • On INSERT/UPDATE/DELETE, update cache via mutate / queryClient.setQueryData — avoid full refetch storms.
  • Show connection status if UX needs it (optional reconnect banner).

Presence / broadcast (chat typing)

Use channel.on("presence", ...) or broadcast only when product requires it; keep payloads small.

Reconnect and offline

  • Supabase Realtime auto-reconnects, but stale state is your problem: on SUBSCRIBED after a disconnect, refetch the initial query so missed events are reconciled.
.subscribe((status) => {
  if (status === "SUBSCRIBED") refetch();
});
  • Optionally show a small "Reconnecting…" indicator if the channel disconnects.

Avoid

  • Leaving channels open after navigation away.
  • Subscribing without RLS (leaks data).
  • Duplicating Realtime + 1s polling for the same data.
  • Subscribing to high-traffic tables without a filter.

Checklist

  • Cleanup in useEffect return.
  • RLS verified for subscribed rows.
  • Cache stays consistent with hook layer.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.