For AI agents: the documentation index is at llms.txt. Guide pages have a markdown version at the same path with .md appended, before any query string or fragment.
REPLICA IDENTITY to FULL (e.g., ALTER TABLE your_table REPLICA IDENTITY FULL;).AsyncStream. Get the channel with await supabase.channel(_:), open the streams, call try await channel.subscribe(), then iterate. To stop listening, cancel the task that iterates the stream.subscribe(): they turn presence on for the join. A presence stream opened after the join, while presence is off, reports an issue and ends at once. A Postgres changes stream opened after the join makes the channel leave and join again with the new binding.subscribe() has no attempt limit. It retries until the join succeeds, fails for good, or its task is cancelled. It throws a RealtimeError when a retry cannot help, for example when an RLS policy refuses the join.broadcast and track throw a RealtimeError of kind .notSubscribed when the channel is not subscribed.// This client sends the message too, so it must receive its own messages.
let channel = await supabase.channel("room1") {
$0.broadcast.receivesOwnMessages = true
}
let broadcastStream = await channel.broadcasts(event: "cursor-pos")
try await channel.subscribe()
Task {
for await message in broadcastStream {
print("Cursor position received", message.payload)
}
}
try await channel.broadcast(
event: "cursor-pos",
payload: [
"x": Double.random(in: 0...1),
"y": Double.random(in: 0...1)
]
)
struct CursorPosition: Codable {
let x: Double
let y: Double
}
// This client sends the message too, so it must receive its own messages.
let channel = await supabase.channel("room1") {
$0.broadcast.receivesOwnMessages = true
}
let broadcastStream = await channel.broadcasts(event: "cursor-pos")
try await channel.subscribe()
let task = Task {
for await message in broadcastStream {
let position = try message.decode(as: CursorPosition.self)
print("Cursor position received", position)
}
}
try await channel.broadcast(
event: "cursor-pos",
payload: CursorPosition(x: 0.5, y: 0.5)
)
// Stop listening some time later
task.cancel()
let channel = await supabase.channel("my-channel") {
$0.isPrivate = true
$0.broadcast.acknowledgesMessages = true
$0.broadcast.receivesOwnMessages = true
$0.broadcast.replay = .init(
since: Date(timeIntervalSinceNow: -3600),
limit: 100
)
}
let messages = await channel.broadcasts(event: "*")
try await channel.subscribe()
for await message in messages where message.isReplayed {
print("Replayed message: \(message.id?.uuidString ?? "")")
}
struct UserStatus: Codable {
let username: String
}
let channel = await supabase.channel("channelId")
let presenceChanges = await channel.presenceChanges()
try await channel.subscribe()
Task {
for await change in presenceChanges {
let joins = try change.joins.values.flatMap { $0 }.map { try $0.decode(as: UserStatus.self) }
let leaves = try change.leaves.values.flatMap { $0 }.map { try $0.decode(as: UserStatus.self) }
}
}
// Send your own state
try await channel.track(UserStatus(username: "John"))
struct UserStatus: Codable {
let username: String
}
let channel = await supabase.channel("channelId") {
$0.presence.key = userID.uuidString
}
let presenceStates = await channel.presenceStates()
try await channel.subscribe()
// Send your own state
try await channel.track(UserStatus(username: "John"))
for await state in presenceStates {
let online: [String: [UserStatus]] = try state.decode()
print("Online users:", online)
}
let channel = await supabase.channel("channelId")
let changeStream = await channel.postgresChanges(schema: "public")
try await channel.subscribe()
for await change in changeStream {
switch change.kind {
case .delete: print("Deleted: \(change.oldRecord?.values ?? [:])")
case .insert: print("Inserted: \(change.record?.values ?? [:])")
case .update: print("Updated: \(change.oldRecord?.values ?? [:]) with \(change.record?.values ?? [:])")
default: break
}
}
let channel = await supabase.channel("channelId")
let changeStream = await channel.postgresChanges(
schema: "public",
table: "users"
)
try await channel.subscribe()
for await change in changeStream {
switch change.kind {
case .delete: print("Deleted: \(change.oldRecord?.values ?? [:])")
case .insert: print("Inserted: \(change.record?.values ?? [:])")
case .update: print("Updated: \(change.oldRecord?.values ?? [:]) with \(change.record?.values ?? [:])")
default: break
}
}
struct User: Decodable {
let id: Int
let username: String
}
let channel = await supabase.channel("channelId")
let changeStream = await channel.postgresChanges(
of: User.self,
schema: "public",
table: "users"
)
try await channel.subscribe()
for await change in changeStream {
switch change.kind {
case .insert, .update:
do {
let user = try change.row()
print("Upserted: \(user)")
} catch {
print("Could not decode the row: \(error)")
}
case .delete: print("Deleted id: \(change.oldRecord?["id"]?.intValue ?? 0)")
default: break
}
}
let channel = await supabase.channel("channelId")
let insertions = await channel.postgresChanges(
event: .insert,
schema: "public",
table: "users"
)
try await channel.subscribe()
for await insert in insertions {
print("Inserted: \(insert.record?.values ?? [:])")
}
let channel = await supabase.channel("channelId")
let updates = await channel.postgresChanges(
event: .update,
schema: "public",
table: "users"
)
try await channel.subscribe()
for await update in updates {
print("Updated: \(update.oldRecord?.values ?? [:]) with \(update.record?.values ?? [:])")
}
let channel = await supabase.channel("channelId")
let deletions = await channel.postgresChanges(
event: .delete,
schema: "public",
table: "users"
)
try await channel.subscribe()
for await deletion in deletions {
print("Deleted: \(deletion.oldRecord?.values ?? [:])")
}
let channel = await supabase.channel("channelId")
let deletions = await channel.postgresChanges(
event: .delete,
schema: "public",
table: "users",
filter: .eq("id", value: 1)
)
try await channel.subscribe()
for await deletion in deletions {
print("Deleted: \(deletion.oldRecord?.values ?? [:])")
}
let channel = await supabase.channel("channelId")
let insertions = await channel.postgresChanges(
event: .insert,
schema: "public",
table: "users"
)
try await channel.subscribe()
let task = Task {
for await insert in insertions {
print("Inserted: \(insert.record?.values ?? [:])")
}
}
// Some time later
task.cancel()
await supabase.removeChannel(channel)