Swift: Subscribe to channel

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()
      \}
    \}
  \}

Listen to all database changes

supabase.realtime
  .channel("room1")
  .on("postgres_changes", filter: ChannelFilter(event: "*", schema: "*")) \{ message in
    print("Change received!", message.payload)
  \}
  .subscribe()

Listen to a specific table

supabase.realtime
  .channel("room1")
  .on("postgres_changes", filter: ChannelFilter(event: "*", schema: "public", table: "countries")) \{ message in
    print("Change received!", message.payload)
  \}
  .subscribe()

Listen to inserts

supabase.realtime
  .channel("room1")
  .on("postgres_changes", filter: ChannelFilter(event: "INSERT", schema: "public", table: "countries")) \{ message in
    print("Change received!", message.payload)
  \}
  .subscribe()

Listen to updates

supabase.realtime
  .channel("room1")
  .on("postgres_changes", filter: ChannelFilter(event: "UPDATE", schema: "public", table: "countries")) \{ message in
    print("Change received!", message.payload)
  \}
  .subscribe()

Listen to deletes

supabase.realtime
  .channel("room1")
  .on("postgres_changes", filter: ChannelFilter(event: "DELETE", schema: "public", table: "countries")) \{ message in
    print("Change received!", message.payload)
  \}
  .subscribe()

Listen to multiple events

supabase.realtime
  .channel("room1")
  .on("postgres_changes", filter: ChannelFilter(event: "INSERT", schema: "public", table: "countries"), handler: handleRecordInserted)
  .on("postgres_changes", filter: ChannelFilter(event: "DELETE", schema: "public", table: "countries"), handler: handleRecordDeleted)
  .subscribe()

func handleRecordInserted(_ message: Message) \{
  // handle message
\}

func handleRecordDeleted(_ message: Message) \{
  // handle message
\}

Listen to row level changes

supabase.realtime
  .channel("room1")
  .on(
    "postgres_changes",
    filter: ChannelFilter(
      event: "INSERT",
      schema: "public",
      table: "countries",
      filter: "id=eq.200"
    ),
    handler: handleRecordInserted
  )
  .subscribe()

  func handleRecordInserted(_ message: Message) \{
    // handle message
  \}