Skip to content

Realtime subscription takes few seconds to recognize changes after successful subscription #51

Open
@FeNo-Engineering

Description

@FeNo-Engineering

Description

After successfully subscribing to a Realtime channel with channel.Subscribe(), there is approximately a 4-second delay before the subscription actually starts recognizing changes to the subscribed table. This occurs even though the subscription appears to be successful immediately (indicated by successful subscription log messages).

Environment

  • Unity Version: 2022.3.35f1
  • Supabase.Realtime Version: 6.0.4
  • Operating System: Windows
  • Project Type: Unity C# (IL2CPP)

Reproduction Steps

  1. Initialize Supabase Realtime client and ensure socket connection
  2. Subscribe to a table using the following pattern:
//Method-Setup
 public async Task<string> SubscribeToTable(string tableName, string filter, string columnName, bool isCustomer, Action<string> onValueChanged)
 {
     try
     {
         var channel = client.Realtime.Channel($"public:{tableName}");        

         var subscriptionInfo = new SubscriptionInfo
         {
             TableName = tableName,
             ColumnName = columnName,
             Filter = filter,
             Channel = channel
         };

         subscriptionInfo.OnValueChanged += onValueChanged;

         try
         {
             channel.Register(new PostgresChangesOptions(
                 schema: "public",
                 table: tableName,
                 eventType: PostgresChangesOptions.ListenType.All,
                 filter: filter
             ));

             channel.AddPostgresChangeHandler (PostgresChangesOptions.ListenType.All, (sender, response) =>
             {
                 try
                 {
                     if (response?.Payload?.Data?.Record != null)
                     {
                         var recordDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(
                             JsonConvert.SerializeObject(response.Payload.Data.Record)
                         );

                         if (recordDict.TryGetValue(columnName, out object columnValueObj) && columnValueObj != null)
                         {
                             string value = columnValueObj.ToString();
                             Debug.Log($"Column value changed to: {value}");
                             subscriptionInfo.InvokeValueChanged(value);
                         }
                     }
                     else
                     {
                         Debug.Log("Changed payload record resulted in null");
                     }
                 }
                 catch (Exception ex)
                 {
                     Debug.LogError($"Error in change handler: {ex.Message}");
                 }
             });

             await channel.Subscribe();

             activeSubscriptions[subscriptionId] = subscriptionInfo;

             Debug.Log($"Successfully subscribed to {tableName}");
             return subscriptionId;
         }
         catch (Exception ex)
         {
             Debug.LogError($"Error setting up channel: {ex.Message}");
             return null;
         }
     }
     catch (Exception e)
     {
         Debug.LogError($"Subscription setup failed: {e.Message}");
         Debug.LogError($"Stack trace: {e.StackTrace}");
         return null;
     }
 }
 
 //Usage:
    string loginTokenSub = await supabaseManager.SubscribeToTable(
       "login_tokens",
       $"token=eq.{CurrentToken}",
       "status",
       false,
       (newValue) => {
           Debug.Log($"login_tokens value changed to: {newValue}");
       }
   );

// Subscription appears successful here
Debug.Log("Successfully subscribed to login_tokens");

// If a change is made to the table immediately, it won't be recognized
// Need to wait ~4 seconds before changes are detected

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions