-
-
Notifications
You must be signed in to change notification settings - Fork 393
Description
I'm encountering a very strange issue with Realtime channels. A new subscription doesn't seem to work correctly until I explicitly unsubscribe and then immediately resubscribe to the same channel.
Current Code (Does Not Work)
Here is the initial code. The subscription is established, but no events are received.
function subscribeChannel() {
realtimeChannel = supabase
.channel(`ptable_reservations_${ptable_id}`)
.on(
'postgres_changes',
{
event: '*',
schema: 'public',
table: 'ptable_reservations',
filter: `ptable_id=eq.${ptable_id}`
},
(payload: RealtimePostgresChangesPayload<Tables<'ptable_reservations'>>) => {
const { eventType, new: newRecord, old: oldRecord } = payload
console.log(payload)
}
)
.subscribe((status, err) => {
console.log(status, err)
if (status === 'CHANNEL_ERROR') {
supabase.removeChannel(realtimeChannel)
subscribeChannel()
}
})
}
Result:
As you can see in the video, no realtime events are being captured.
2025-08-30.13-15-02.webm
Working Code (Workaround)
However, if I introduce an removeChannel() call before the final subscribe(), it starts working as expected.
let first = true
function subscribeChannel() {
realtimeChannel = supabase
.channel(`ptable_reservations_${ptable_id}`)
.on(
'postgres_changes',
{
event: '*',
schema: 'public',
table: 'ptable_reservations',
filter: `ptable_id=eq.${ptable_id}`
},
(payload: RealtimePostgresChangesPayload<Tables<'ptable_reservations'>>) => {
const { eventType, new: newRecord, old: oldRecord } = payload
console.log(payload)
}
)
.subscribe((status, err) => {
console.log(status, err)
if (first) { // Modified!
first = false
supabase.removeChannel(realtimeChannel)
subscribeChannel()
return
}
if (status === 'CHANNEL_ERROR') {
supabase.removeChannel(realtimeChannel)
subscribeChannel()
}
})
}
Result:
Now, the realtime events are received correctly.
2025-08-30.13-15-37.webm
As demonstrated, the subscription only becomes active after this unsubscribe/resubscribe cycle. This behavior is unexpected and seems like a potential bug. I can't figure out why the initial subscription fails to receive events on its own.
ubuntu 24.04 / 22.04
'@supabase/ssr':
specifier: ^0.6.1
version: 0.6.1(@supabase/[email protected])
'@supabase/supabase-js':
specifier: ^2.49.4
version: 2.54.0
node: v24.1.0