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

Unable to pass arguments (i.e. auto_reconnect) to AsyncRealtimeClient #927

Closed
yorickvanzweeden opened this issue Sep 17, 2024 · 2 comments · Fixed by #938
Closed

Unable to pass arguments (i.e. auto_reconnect) to AsyncRealtimeClient #927

yorickvanzweeden opened this issue Sep 17, 2024 · 2 comments · Fixed by #938

Comments

@yorickvanzweeden
Copy link

Issue

I seem to be unable to pass arguments, such as auto_reconnect to the AsyncRealtimeClient. Therefore, I need to patch the client after initialization.

    client = await acreate_client(settings.SUPABASE_URL, settings.SUPABASE_SERVICE_ROLE_SECRET)
    client.realtime = AsyncRealtimeClient(client.realtime_url, token=client.supabase_key, auto_reconnect=True)
    return client

The problem

The acreate_client accepts ClientOptions, which has a realtime field with type Optional[Dict[str, Any]]. These will be passed to _init_realtime_client.

self.realtime = self._init_realtime_client(
realtime_url=self.realtime_url,
supabase_key=self.supabase_key,
options=options.realtime if options else None,
)

@staticmethod
def _init_realtime_client(
realtime_url: str, supabase_key: str, options: Optional[Dict[str, Any]]
) -> AsyncRealtimeClient:
"""Private method for creating an instance of the realtime-py client."""
return AsyncRealtimeClient(
realtime_url, token=supabase_key, params=options or {}
)

However, the options are passed to params, and I cannot pass the auto_reconnect flag (or other attributes) via the supabase module.

permalink

class AsyncRealtimeClient:
    def __init__(
        self,
        url: str,
        token: str,
        auto_reconnect: bool = False,
        params: Dict[str, Any] = {},
        hb_interval: int = 30,
        max_retries: int = 5,
        initial_backoff: float = 1.0,
    ) -> None:

Suggestion

@staticmethod
def _init_realtime_client(
    realtime_url: str, supabase_key: str, options: Optional[Dict[str, Any]] = None
) -> AsyncRealtimeClient:
    """Private method for creating an instance of the realtime-py client."""
    if options is None:
        options = {}
    return AsyncRealtimeClient(
        realtime_url, token=supabase_key, **options
    )

Unpack options immediately. This would allow configuration such as:

    options = ClientOptions(
        realtime={
            "auto_reconnect": True,
        }
    )
    client = await acreate_client(settings.SUPABASE_URL, settings.SUPABASE_SERVICE_ROLE_SECRET, options)
@krishpy99
Copy link

Hi is there a way I can test this locally after I fix this? I am new to supabase and open source.. I have used supabase as a client but I am unsure of the use-case as a realtime client and how I should test the same.

@yorickvanzweeden
Copy link
Author

yorickvanzweeden commented Sep 26, 2024

Hi is there a way I can test this locally after I fix this? I am new to supabase and open source.. I have used supabase as a client but I am unsure of the use-case as a realtime client and how I should test the same.

Not predictably, you need to wait hours until it randomly disconnects, and it might just keep silent. So you need to trigger it via database changes or realtime messages to verify the disconnect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants