By default, Broadcast and Presence are enabled for all projects.
By default, listening to database changes is disabled for new projects due to database performance and security concerns. You can turn it on by managing Realtime's replication.
You can receive the "previous" data for updates and deletes by setting the table's REPLICA IDENTITY to FULL (e.g., ALTER TABLE your_table REPLICA IDENTITY FULL;).
Row level security is not applied to delete statements. When RLS is enabled and replica identity is set to full, only the primary key is sent to clients.
Examples
Listen to broadcast messages
let channel = supabase
.realtime
.channel("room1")
channel
.on("broadcast", filter: ChannelFilter(event: "cursor-pos")) \{ message in
print("Cursor position received!", message.payload)
\}
.subscribe \{ status, error in
if status == .subscribed \{
Task \{
await channel.send(
type: .broadcast,
event: "cursor-pos",
payload: ["x": Double.random(in: 0...1), "y": Double.random(in: 0...1)]
)
\}
\}
\}
Listen to presence sync
let channel = supabase.realtime.channel("room1")
channel
.on("presence", filter: ChannelFilter(event: "sync")) \{ _ in
print("Synced presence state: ", channel.presenceState())
\}
.subscribe \{ status, error in
if status == .subscribed \{
Task \{
await channel.track(["online_at": Date().ISO8601Format()])
\}
\}
\}
Listen to presence join
let channel = supabase.realtime.channel("room1")
channel
.on("presence", filter: ChannelFilter(event: "join")) \{ message in
print("Newly joined presences: ", message.payload)
\}
.subscribe \{ status, error in
if status == .subscribed \{
Task \{
await channel.track(["online_at": Date().ISO8601Format()])
\}
\}
\}
Listen to presence leave
let channel = supabase.realtime.channel("room1")
channel
.on("presence", filter: ChannelFilter(event: "leave")) \{ message in
print("Newly left presences: ", message.payload)
\}
.subscribe \{ status, error in
if status == .subscribed \{
Task \{
await channel.track(["online_at": Date().ISO8601Format()])
await channel.untrack()
\}
\}
\}