S
Supabase•2mo ago
gigatalk

After update package, realtime subscription not work.

I updated package @supabase/ssr": "^0.6.1", "@supabase/supabase-js": "^2.50.3", -> "@supabase/ssr": "^0.7.0","@supabase/supabase-js": "^2.57.4", And then, realtime subscription not work some times when i reload pages. But after i changed to use new 'publishable key' system from legacy anon key system. The friquency of error reduced dramatically. Why this happen? I'm using supabase SSR. Does it related to supabase.realtime.setAuth? Should i setAuth again while getting supabase object from +layout.ts?
21 Replies
ihm40
ihm40•2mo ago
I think there are two things going on here. For the second one make sure that the PUBLISHABLED_KEY is being used correctly like this guide states https://supabase.com/docs/guides/auth/server-side/nextjs
ihm40
ihm40•2mo ago
It may be the case that your environment variables are out of alignment in your deployment
ihm40
ihm40•2mo ago
Another user raised the first issue as a bug here https://github.com/supabase/ssr/issues/122. There is a solution suggested further down. Might be worth seeing if it helps?
GitHub
supabase/ssr
Supabase clients for use in server-side rendering frameworks. - supabase/ssr
garyaustin
garyaustin•2mo ago
So several users are having issues with supabase-js 2.54 and later. What is fixing it is to add supabase.realtime.setAuth(user.session.access_token) before subscribing. Not clear if the access_token is required or just setAuth() is sufficient. https://discord.com/channels/839993398554656828/1414876335662301194/1414964273284780133
House
House•3w ago
Same here just confirming this happens for broadcast. Realtime was working fine for me but after updating to the new asym keys suddenly it stops working. You can see in the websocket it's trying to use the publishable_key The fix unfortunately didn't work for me šŸ™ // Get the user's session token const { data: { session }, } = await supabase.auth.getSession(); if (!session?.access_token) { console.error('No access token available for realtime auth'); return; } // Set auth with the access token await supabase.realtime.setAuth(session.access_token);
garyaustin
garyaustin•3w ago
Also try with just setAuth(). Some have had that work. Otherwise an issue in github supabase/supabase-js or realtime-js... or support.
House
House•3w ago
Oh I had await supabase.realtime.setAuth(); before lol, thanks for trying to help though. Yea will create an issue.
garyaustin
garyaustin•3w ago
No use await. Just don't put the session token in. Also log out your session token to make sure it is a real JWT. It should be of course.
House
House•3w ago
Oh you mean do both, like await setAuth(), and then set session.access_token?
House
House•3w ago
Tried that too, did't work. Created issue here: https://github.com/supabase/realtime/issues/1561
GitHub
Subscribing to broadcasts does not work with the new non-JWT publis...
Only tested in local. Previously I used setAuth() before subscribing and it was working await supabase.realtime.setAuth(); Now, this is what I get in console when I try to subscribe to a channel: W...
garyaustin
garyaustin•3w ago
await supabase.realtime.setAuth()
House
House•3w ago
const setupSubscription = async () => {
try {
if (!mounted) return;

await supabase.realtime.setAuth();

// Get the user's session token
const {
data: { session },
} = await supabase.auth.getSession();

if (!session?.access_token) {
console.error('No access token available for realtime auth');
return;
}

// Set auth with the access token
await supabase.realtime.setAuth(session.access_token);

if (!mounted) return;

const channel = supabase.channel(channelName, {
config: { private: true },
});

channelRef.current = channel;
const setupSubscription = async () => {
try {
if (!mounted) return;

await supabase.realtime.setAuth();

// Get the user's session token
const {
data: { session },
} = await supabase.auth.getSession();

if (!session?.access_token) {
console.error('No access token available for realtime auth');
return;
}

// Set auth with the access token
await supabase.realtime.setAuth(session.access_token);

if (!mounted) return;

const channel = supabase.channel(channelName, {
config: { private: true },
});

channelRef.current = channel;
garyaustin
garyaustin•3w ago
Exactly that but with session.access_token not part of setAuth() Just setAuth(). I doubt it matters but several users did it that way to fix the issue.
House
House•3w ago
Sorry mate I don't really follow lol. What am I missing here
garyaustin
garyaustin•3w ago
// Set auth with the access token
await supabase.realtime.setAuth() //without access token;
// Set auth with the access token
await supabase.realtime.setAuth() //without access token;
As I said I don't think it will matter, but that is the I've seen others doing it.
House
House•3w ago
Oh yea I have it here: await supabase.realtime.setAuth(); I am probably not explaining myself correctly but I think I did in the bug report. So before I had "await supabase.realtime.setAuth(); " and nothing else, and it was working fine. Then after publishable_keys, this stopped working, so I tried to add your fix of set token. Then I tried doing 2 together lol. Yea it looks like the container in Docker is expecting JWT.
garyaustin
garyaustin•3w ago
So this is local... Could very well be an issue with that. I tested publishable keys and they work on hosted.
House
House•3w ago
Ah well that's good to know. I'll wait for the fix before I push to prod then. Interesting!! Although local did get publishable keys recently. I believe hosted gets stuff first and had new asym keys for a while.
garyaustin
garyaustin•3w ago
Could be only part of the local version got fixed. Even on hosted right after launch there were issues with storage and the publishable keys that had to get fixed.
House
House•3w ago
Thanks again for being super helpful mate. I'll keep a watch here and on my bug report to see if I can help devs debug
Arek Chiluta
Arek Chiluta•2w ago
I have a similar issue and I've added my comment here - https://github.com/supabase/ssr/issues/122
GitHub
Title: Realtime updates not received on first page load when using ...
I’m experiencing an issue where realtime updates are not received on the first page load when using createBrowserClient (e.g. from @supabase/ssr or @supabase/auth-helpers-nextjs) in Next.js. The co...

Did you find this page helpful?