Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FeNo-Engineering opened this issue Dec 16, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@FeNo-Engineering
Copy link

FeNo-Engineering commented Dec 16, 2024

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
@FeNo-Engineering FeNo-Engineering added the bug Something isn't working label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant