https://supabase.com/ logo
#help
Title
# help
n

NARCISO

04/30/2022, 12:57 AM
Hi everyone, I am currently encountering issues with
subscriptions
. I'm using
NextJS
and I use a hook in 2 different pages to setup the subscription that is basically this (copied from the
nextjs-slack-clone
official example):
Copy code
...
  useEffect(() => {
    const messageListener = sb
      .from<Message>("message")
      .on("INSERT", (payload) => setTempNewMessage(payload.new))
      .subscribe((event, e) => {
        console.warn(`[Message] Listener ${event}`, messageListener);
        if (e) {
          console.error("[Message] Listener error", e);
        }
      });

    console.error("STARTED LISTENING...", messageListener);
    // Cleanup on unmount
    return () => {
      messageListener
        .unsubscribe()
        .receive("ok", () => console.error("!SUBSCRIPTION REMOVED!"));
    };
  }, []);
At the first page load, the subscription is all good and works perfectly. But when I do
router.push
to the other page, it unsubscribe correctly but the new subscription starts
CLOSED
and even the
RETRYING_AFTER_TIMEOUT
does nothing. Any ideas?
n

Needle

04/30/2022, 12:57 AM
Hello @NARCISO! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
🆕 Subscription not starting after page routing
g

garyaustin

04/30/2022, 1:08 AM
I don't use Next.js but if both useEffects are called, the subscription would be combined to 1 as only one connection is allowed. Then when you leave page if you close subscription it closes both. BUT I don't know if both get called before you do the router.push or 1 is called and then the 2nd is called only after router.push.
n

Needle

04/30/2022, 1:08 AM
Subscription not starting after page routing
n

NARCISO

04/30/2022, 1:09 AM
I have this logs
It seems that it's been done in sequence, but maybe not
-------- These are the messages from the websocket. The first
2 messages
are from the first page load. The
third
message is the
leave
from the
unsubscribe
. Then the
fourth
is the
join
from the new page and seeing the
fifth
message should be good. But then the
sixth
closes the subscription for no reason
I wonder if there is or would be a good option to be able to set an ID on the subscription so to (maybe) be able to handle these cases. But maybe I'm doing it wrong
g

garyaustin

04/30/2022, 1:16 AM
I agree it looks in parallel, but man .008 seconds from telling server leave to join... not sure what that would do.
n

NARCISO

04/30/2022, 1:16 AM
I'll try with a delay of 1 sec and let's see
g

garyaustin

04/30/2022, 1:17 AM
I meant series not parallel
n

NARCISO

04/30/2022, 1:17 AM
Yeah yeah
Damn
It works with 1 sec delay
I don't know if it's a good solution keeping 1 sec delay to start the realtime tho ahaha
g

garyaustin

04/30/2022, 1:21 AM
I think that is a bug then, you might report in github realtime
n

NARCISO

04/30/2022, 1:22 AM
I will
Do you have any idea to do this better tho? Maybe I could play around with
supabase.getSubscriptions()
and see if I can for example see the subscription still not closed at all and wait? I don't know haha
Should I open it on supabase/realtime or supabase/supabase-js?
g

garyaustin

04/30/2022, 1:25 AM
So if you don't kill the subscription, and call it again it just gets replaced
realtime is the place
it looks to me like the server glitches
n

NARCISO

04/30/2022, 1:29 AM
I tried removing the
unsubscribe
but it does not replace it. Actually,
NextJS
seems to close the subscription automatically.
Allright I will report it there then
g

garyaustin

04/30/2022, 1:31 AM
Yeah, sorry as I said I don't use nextJS... supabase.js/realtime.js though is supposed to only allow 1 subscription to same "event" type on a table/filter.
n

NARCISO

04/30/2022, 1:31 AM
Maybe it's something @supabase/supabase-auth-helpers is doing
g

garyaustin

04/30/2022, 1:31 AM
But I guess if you have multiple handlers on it they would both tirgger
I was going thru that code for token stuff and it did not seem to deal with realtime at all.
n

NARCISO

04/30/2022, 1:34 AM
well then I don't know ahah
g

garyaustin

04/30/2022, 1:35 AM
But at least report your traces in realtime and they can figure out where the issue is. You have some nice traces that help.
n

NARCISO

04/30/2022, 1:38 AM
Yeah I will include the screenshots I sent in this chat
@garyaustin FYI this is the url of the issue: https://github.com/supabase/realtime/issues/254
Thanks for the help! You are fantastic!
n

Needle

04/30/2022, 2:05 AM
Thread was archived by @NARCISO. Anyone can send a message to unarchive it.