Skip to content

Instantly share code, notes, and snippets.

@bidah
Last active September 14, 2024 13:22
Show Gist options
  • Save bidah/cc77238fd863f202ed09a7a3d7145fbc to your computer and use it in GitHub Desktop.
Save bidah/cc77238fd863f202ed09a7a3d7145fbc to your computer and use it in GitHub Desktop.
import { supabase } from "@/lib/supabase";
import { useAuth } from "@/services/auth/useAuth";
import { useEffect } from "react";
import { trpc } from "@/utils/trpc";
export function useRealtime() {
// your auth setup to get current user id
const { session } = useAuth();
const userId = session?.user.id;
const utils = trpc.useUtils();
useEffect(() => {
if (!userId) return;
const channel = supabase
.channel("done-capsules")
.on(
"postgres_changes",
{
event: "*",
schema: "public",
table: "table_name",
filter: `user_id=eq.${userId}`,
},
(payload) => {
// Invalidate the getTranscript query for the updated transcript
console.log("-> realtime triggered");
// this is a sample replace `myQuery` with your own
utils.myQuery.invalidate();
},
)
.subscribe();
return () => {
supabase.removeChannel(channel);
};
}, [userId, utils]);
}
export default function RealtimeProvider({
children,
}: {
children: React.ReactNode;
}) {
useRealtime();
return <>{children}</>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment