C#: Subscribe to channel

Subscribe to realtime changes in your database.

Examples

Listen to broadcast messages

class CursorBroadcast : BaseBroadcast
\{
    [JsonProperty("cursorX")]
    public int CursorX \{get; set;\}

    [JsonProperty("cursorY")]
    public int CursorY \{get; set;\}
\}

var channel = supabase.Realtime.Channel("any");
var broadcast = channel.Register<CursorBroadcast>();
broadcast.AddBroadcastEventHandler((sender, baseBroadcast) =>
\{
    var response = broadcast.Current();
\});

await channel.Subscribe();

// Send a broadcast
await broadcast.Send("cursor", new CursorBroadcast \{ CursorX = 123, CursorY = 456 \});

Listen to presence sync

  class UserPresence : BasePresence
  \{
      [JsonProperty("cursorX")]
      public bool IsTyping \{get; set;\}

      [JsonProperty("onlineAt")]
      public DateTime OnlineAt \{get; set;\}
  \}

  var channel = supabase.Realtime.Channel("any");
  var presenceKey = Guid.NewGuid().ToString();
  var presence = channel.Register<UserPresence>(presenceKey);
  presence.AddPresenceEventHandler(EventType.Sync, (sender, type) =>
  \{
      Debug.WriteLine($"The Event Type: \{type\}");
      var state = presence.CurrentState;
  \});

  await channel.Subscribe();

  // Send a presence update
  await presence.Track(new UserPresence \{ IsTyping = false, OnlineAt = DateTime.Now \});

Listening to a specific table

await supabase.From<City>().On(ListenType.All, (sender, change) =>
\{
    Debug.WriteLine(change.Payload.Data);
\});

Listen to all database changes

var channel = supabase.Realtime.Channel("realtime", "public", "*");

channel.AddPostgresChangeHandler(ListenType.All, (sender, change) =>
\{
    // The event type
    Debug.WriteLine(change.Event);
    // The changed record
    Debug.WriteLine(change.Payload);
\});

await channel.Subscribe();

Listening to inserts

await supabase.From<City>().On(ListenType.Inserts, (sender, change) =>
\{
    Debug.WriteLine(change.Payload.Data);
\});

Listening to updates

await supabase.From<City>().On(ListenType.Updates, (sender, change) =>
\{
    Debug.WriteLine(change.Payload.Data);
\});

Listening to deletes

await supabase.From<City>().On(ListenType.Deletes, (sender, change) =>
\{
    Debug.WriteLine(change.Payload.Data);
\});

Listening to row level changes

var channel = supabase.Realtime.Channel("realtime", "public", "countries", "id", "id=eq.200");

channel.AddPostgresChangeHandler(ListenType.All, (sender, change) =>
\{
    // The event type
    Debug.WriteLine(change.Event);
    // The changed record
    Debug.WriteLine(change.Payload);
\});

await channel.Subscribe();