I have a vanilla Javascript app using Supabase a broadcast channel to keep things in sync.
const client = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
const channel = client.channel('test-channel', {});
channel
.on(
'broadcast',
{ event: 'timer_update' },
(payload) => messageReceived(payload)
)
.subscribe((status, err) => {
switch (status) {
case 'SUBSCRIBED':
$("#msg").html('Connected!');
break;
case 'CHANNEL_ERROR':
console.log(err)
$("#msg").html(`There was an error subscribing to channel: ${err.message}`)
break;
case 'TIMED_OUT':
console.log(err)
$("#msg").html('Realtime server did not respond in time.')
break;
case 'CLOSED':
console.log(err)
$("#msg").html('Realtime channel was unexpectedly closed.')
break;
}
})
Sometimes (can be following a browser window sleeping or other understandable breaks in connection) my subscribe listener receives a status of CHANNEL_ERROR
and no message. After that point I can still broadcast to the channel, but no longer receive messages.
I have tried doing a channel = client.removeChannel('test-channel');
so I could re-create the subscription, but that gives an error:
supabase-js@2:7 Uncaught (in promise) TypeError: e.unsubscribe is not a function
at x.removeChannel (supabase-js@2:7:80446)
at t.default.removeChannel (supabase-js@2:7:97012)
I guess what I’d like to do is in the event of the error, or after a check when the window regains focus, I have a way to elegantly reconnect to listen again.