supabase / supabase-py

Python Client for Supabase. Query Postgres from Flask, Django, FastAPI. Python user authentication, security policies, edge functions, file storage, and realtime data streaming. Good first issue.
https://supabase.com/docs/reference/python
MIT License
1.72k stars 203 forks source link

Realtime is not available when using sync client #909

Closed Player-14 closed 2 months ago

Player-14 commented 2 months ago

Bug report

Describe the bug

Realtime is not available when using the sync client in supabase-py. The traceback is:

Traceback (most recent call last):
  File "C:\Users\USER\Desktop\Apps\ChatRock\chatrock\main.py", line 86, in <module>
    messages = supabase.channel("messages")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\Desktop\Apps\ChatRock\venv\Lib\site-packages\supabase\_sync\client.py", line 204, in channel
    return self.realtime.channel(topic, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\Desktop\Apps\ChatRock\venv\Lib\site-packages\realtime\_sync\client.py", line 39, in channel
    raise NotImplementedError(NOT_IMPLEMENTED_MESSAGE)
NotImplementedError: This feature isn't available in the sync client. You can use the realtime feature in the async client only.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

from supabase import create_client, Client

supabase: Client = create_client("SUPABASE_URL", "SUPABASE_KEY")
messages = supabase.channel("messages") # This triggers the error
  1. Create supabase project with a realtime table called "messages"
  2. Input URL and key
  3. Run code in python
  4. See error

Expected behavior

The module should just subscribe to the channel without tracebacks.

System information

Additional context

I am aware that realtime is available in the async class, but I prefer the sync class for ease of use.

silentworks commented 2 months ago

It's not a bug, its a feature. This won't be changed until we find a way to make websockets work without asyncio.

sleeyax commented 1 month ago

Where is the async python client even documented? I can't for the life of me find it.

EDIT:

So it turns out the async client is pretty much undocumented and any documentation regarding the realtime functionality of the python package on the main supabase docs site is also outdated/wrong.

I finally found this half-decent example: https://github.com/supabase/realtime-py/blob/main/example/app.py#L66.

Example using supabase-py:

supabase: AClient = await acreate_client(getenv("SUPABASE_URL"), getenv("SUPABASE_KEY"))

def my_callback(payload):
       print(payload)

await supabase.realtime.connect()

await (supabase.realtime
           .channel("my_channel")
           .on_postgres_changes("*", schema="public", table="my_table", callback=my_callback)
           .subscribe())

await supabase.realtime.listen()

Don't forget the await supabase.realtime.listen() at the end, took me hours of debugging strange timeout errors to realize this is required!