-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When used with react strict mode, the realtime database does not subscribe properly. #169
Comments
Is this with React 18? React 18's Strict mode has been nothing but trouble for me since it started running useEffect twice in localhost |
Yes, I'm using React 18. The above code should work fine if useEffect is called twice. But in reality, it does not. |
Same issue here, Vite with React 18 and a brand new project. Strict Mode is enabled. useEffect(() => {
const sub = supabase
.from('message')
.on('*', (message) => {
console.log('New Message');
handleNewMessage(message.new);
})
.subscribe();
return () => {
supabase.removeSubscription(sub);
};
}, []); |
got it! Sending this to the realtime-js repo 👍 |
In my case, I upgraded to Next.js v12.2.3 and it solved the problem. |
Closing this as it's no longer an issue with a later version of next js. |
This is still an issue with NextJS v12.2.5 and supabase-js v1.35.6 |
Using this workaround, for the time being, solves the issue => useEffect(() => {
// This is a hack for React >= v18 in Strict mode
// ensures to always call on the last mount and
// correctly remove subscription on unmount
let subscription = null;
const timer = setTimeout(() => subscription = subscribeToTable(), 1000);
function subscribeToTable() {
return (
supabase
.from("my_table")
.on("*", (payload) => {
console.log('Change received!', payload)
})
.subscribe((msg) => {
if (msg === 'SUBSCRIBED') {
isSubscribed.current = true;
}
})
);
}
return () => {
if (!subscription) {
return clearTimeout(timer);
}
supabase.removeSubscription(subscription);
isSubscribed.current = false;
};
}, []); |
The issue seems to not be a react specific issue and can be observed in a vanilla typescript setup. See this minimal reproduction repro with As others noted useEffect(() => {
const channel = supabase
.channel("db-changes")
.on("postgres_changes", { event: "*", schema: "*" }, (payload: any) =>
console.log("Postgres change", payload)
);
channel.subscribe();
return () => {
channel.unsubscribe()
};
}, []); Calling above const channel1 = supabase
.channel("db-changes")
.on("postgres_changes", { event: "*", schema: "*" }, (payload: any) =>
console.log("Postgres change channel 1", payload)
);
channel1.subscribe();
channel1.unsubscribe();
const channel2 = supabase
.channel("db-changes")
.on("postgres_changes", { event: "*", schema: "*" }, (payload: any) =>
console.log("Postgres change channel 2", payload)
);
channel2.subscribe(); Obviously it does not make too much sense to write above code. But there are use cases where exactly this will happen. For example when there are two components "Top Upvoted Posts" and "Latest Posts" on a page. When each of these two components subscribe to the postgres-changes for the table Running this code, the server still sends the change events but neither Most of the time the Sometimes the Regardless if the |
Hey everyone, I have fixed this issue in Also, please use either |
@chiubaca just wanted to check if you tried it with cleanup and |
@chiubaca i forked your stackblitz and did some changes, now it's working. Problem was that the useEffect destructor got called immediately and the channel has been removed, but your ref has not been reset. The flow of the double called useEffect is this:
Keep in mind the double call to useEffect is only during development. Although this might happen in production for example if a user switches pages fast (eg: A->B->A). @w3b6x9 i noticed another thing: Merging with spread operators does only a shallow merge. If realtime-js/src/RealtimeChannel.ts Lines 56 to 62 in c32db96
|
@pixtron that's fine as the Realtime server will take the absence to mean |
thanks so much for taking the to fix my stackblitz example @pixtron. And your explanation reassured me that my mental model of the double I was wondering if the |
@chiubaca i just cleared the interval in my fork. If you don't clear it. the interval runs forever. Eg if you go to "Send broadcast" and then after ~4s "back to home" you will still see an Maybe it makes sense to move this conversation over to discord - feel free to ping me there if you have any further questions. |
I have removed |
Bug report
Describe the bug
When we develop with the React strict mode, we can't build subscriptions correctly.
To Reproduce
Turn on strict mode:
Using subscriptions in components:
Expected behavior
In strict mode, useEffect is triggered twice. In theory, it should quickly establish and close a connection once, and then establish it again.
However, in practice, it is not possible to subscribe properly. In strict mode,
subscribe()
will receive the CLOSED signal directly at the first time.Screenshots
The output in the console is as follows:
System information
Additional context
In supabase/supabase#7771 it is suggested that turning on strict mode can cause problems.
The text was updated successfully, but these errors were encountered: