S
Supabase•3w ago
aepryus

Swift App not receiving callbacks on the onBroadcast(event:) for Broadcast on Database

I am not seeing any call back messages from real-time in my Hello, World swift app. The app is able to connect and insert rows of data, but the onBroadcast callbacks are never triggered. I can following the channel from the dashboard and see the messages coming across. I can also use:
let myChannel = await supabase.channel("test-channel")

// Listen for broadcast messages
let broadcastStream = await myChannel.broadcast(event: "shout") // Listen for "shout". Can be "*" to listen to all events

await myChannel.subscribe()

for await event in broadcastStream {
print(event)
}
let myChannel = await supabase.channel("test-channel")

// Listen for broadcast messages
let broadcastStream = await myChannel.broadcast(event: "shout") // Listen for "shout". Can be "*" to listen to all events

await myChannel.subscribe()

for await event in broadcastStream {
print(event)
}
...and see the messages coming across. But at the same time, nothing on the onBroadcast callbacks.
let _ = channel.onBroadcast(event: "*") { (payload: [String: AnyJSON]) in
print("🌟 ANY broadcast received: \(payload)")
}
let _ = channel.onBroadcast(event: "*") { (payload: [String: AnyJSON]) in
print("🌟 ANY broadcast received: \(payload)")
}
1 Reply
aepryus
aepryusOP•3w ago
Here is a discussion with @garyaustin from another therad: https://discord.com/channels/839993398554656828/1385316542970990693/1402316770676572263 This is Supabase 2.31.1.
await supabase.realtimeV2.setAuth()

// Create private channel
let channel = supabase.channel("aether_changes") { (config: inout RealtimeChannelConfig) in
config.isPrivate = true
}

...

// Also add this to catch ANY broadcasts
let _ = channel.onBroadcast(event: "*") { (payload: [String: AnyJSON]) in
print("🌟 ANY broadcast received: \(payload)")
}

try await channel.subscribeWithError()
await supabase.realtimeV2.setAuth()

// Create private channel
let channel = supabase.channel("aether_changes") { (config: inout RealtimeChannelConfig) in
config.isPrivate = true
}

...

// Also add this to catch ANY broadcasts
let _ = channel.onBroadcast(event: "*") { (payload: [String: AnyJSON]) in
print("🌟 ANY broadcast received: \(payload)")
}

try await channel.subscribeWithError()
Figured it out: channel.onBroadcast does not strongly hold the subscription. So, it was being dropped immediately and ultimately no listeners were registered.

Did you find this page helpful?