© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago•
5 replies
Hermes

Unsubscribe from a realtime channel

How exactly does a client unsub from a realtime channel (Postgres CDC)?
From the docs, there is
removeChannel()
removeChannel()
which unsubs and 'removes' the channel from client.
What does the remove part mean, if multiple clients are connected to the channel, and one client does a
removeChannel()
removeChannel()
with that channels' name, does it also end the channel for the other clients?

And there is also a
supabase.channel().unsubscribe()
supabase.channel().unsubscribe()
which 'unsubscribes from server events and instructs server to terminate'...

// snippet from CommentItem component
// LISTEN FOR WHEN THE COMMENT IS UPDATED OR DELETED
    useEffect(() => {
        const { id: commentID } = commentData;
        const channelName = `public:comments:id=eq.${commentID}`;
        supabase
            .channel(channelName)
            .on(
                "postgres_changes",
                {
                    event: "UPDATE",
                    schema: "public",
                    table: "comments",
                    filter: `id=eq.${commentID}`,
                },
                onCommentUpdated
            )
            .on(
                "postgres_changes",
                {
                    event: "DELETE",
                    schema: "public",
                    table: "comments",
                    filter: `id=eq.${commentID}`,
                },
                onCommentDeleted
            )
            .subscribe();
        return () => supabase.removeChannel(channelName);
    }, [commentData, onCommentDeleted, onCommentUpdated]);
// snippet from CommentItem component
// LISTEN FOR WHEN THE COMMENT IS UPDATED OR DELETED
    useEffect(() => {
        const { id: commentID } = commentData;
        const channelName = `public:comments:id=eq.${commentID}`;
        supabase
            .channel(channelName)
            .on(
                "postgres_changes",
                {
                    event: "UPDATE",
                    schema: "public",
                    table: "comments",
                    filter: `id=eq.${commentID}`,
                },
                onCommentUpdated
            )
            .on(
                "postgres_changes",
                {
                    event: "DELETE",
                    schema: "public",
                    table: "comments",
                    filter: `id=eq.${commentID}`,
                },
                onCommentDeleted
            )
            .subscribe();
        return () => supabase.removeChannel(channelName);
    }, [commentData, onCommentDeleted, onCommentUpdated]);


I just want that when the commentItem component unmounts, it should stop listening for those postgres changes on the comments table.
image.png
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Unable to resubscribe to a channel after unsubscribe
SupabaseSSupabase / help-and-questions
3mo ago
Should you ever use RealtimeClient.channel.unsubscribe()?
SupabaseSSupabase / help-and-questions
5mo ago
Disconnecting and reconnecting from realtime channels
SupabaseSSupabase / help-and-questions
3y ago
Detect disconnection of realtime channel
SupabaseSSupabase / help-and-questions
4y ago