The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs(python): add reference docs for python realtime (#28680)
Guilherme Souzaf2ab50ea4524
diff --git a/apps/docs/content/guides/realtime/broadcast.mdx b/apps/docs/content/guides/realtime/broadcast.mdxindex 40d5cd2a34a..e741947d227 100644--- a/apps/docs/content/guides/realtime/broadcast.mdx+++ b/apps/docs/content/guides/realtime/broadcast.mdx@@ -73,6 +73,17 @@ Go to your Supabase project's [API Settings](https://supabase.com/dashboard/proj```</TabPanel>+ <TabPanel id="python" label="Python">++ ```python+ from supabase import create_client++ URL = "https://<project>.supabase.co"+ KEY = "<your-anon-key>"+ supabase = create_client(URL, KEY)+ ```++ </TabPanel></Tabs>### Listening to broadcast messages@@ -159,6 +170,25 @@ You can provide a callback for the `broadcast` channel to receive message. In th```</TabPanel>+ <TabPanel id="python" label="Python">+ ```python+ # Join a room/topic. Can be anything except for 'realtime'.+ channel_a = supabase.channel('room-1')++ # Simple function to log any messages we receive+ def message_received(payload):+ print(f"Broadcast received: {payload}")++ # Subscribe to the Channel+ channel_a+ .on_broadcast(+ 'test',+ message_received+ )+ .subscribe()+ ```++ </TabPanel></Tabs>### Sending broadcast messages@@ -250,6 +280,28 @@ You can provide a callback for the `broadcast` channel to receive message. In th```</TabPanel>+ <TabPanel id="python" label="Python">++ We can send Broadcast messages using `channel_b.send_broadcast()`. Let's set up another client to send messages.++ ```python+ # Join a room/topic. Can be anything except for 'realtime'.+ channel_b = supabase.channel('room-1')++ def on_subscribe(status, err):+ if status != RealtimeSubscribeStates.SUBSCRIBED:+ return++ # Send a message once the client is subscribed+ channel_b.send_broadcast(+ 'test',+ { "message": 'hello, world' },+ )++ channel_b.subscribe(on_subscribe)+ ```++ </TabPanel></Tabs>Before sending messages we need to ensure the client is connected, which we have done within the `subscribe()` callback.@@ -374,6 +426,33 @@ You can pass configuration options while initializing the Supabase Client.```</TabPanel>+ <TabPanel id="python" label="Python">++ By default, broadcast messages are only sent to other clients. You can broadcast messages back to the sender by setting Broadcast's `self` parameter to `True`.++ ```python+ # Join a room/topic. Can be anything except for 'realtime'.+ my_channel = supabase.channel('room-2', {"config": {"broadcast": {"self": True}}})++ my_channel.on_broadcast(+ 'test-my-messages',+ lambda payload: print(payload)+ )++ def on_subscribe(status, err):+ if status != RealtimeSubscribeStates.SUBSCRIBED:+ return++ # Send a message once the client is subscribed+ channel_b.send_broadcast(+ 'test-my-messages',+ { "message": 'talking to myself' },+ )++ my_channel.subscribe(on_subscribe)+ ```++ </TabPanel></Tabs>### Acknowledge messages@@ -466,6 +545,9 @@ You can pass configuration options while initializing the Supabase Client.```</TabPanel>+ <TabPanel id="python" label="Python">+ Unsupported in Python yet.+ </TabPanel></Tabs>Use this to guarantee that the server has received the message before resolving `channelD.send`'s promise. If the `ack` config is not set to `true` when creating the channel, the promise returned by `channelD.send` will resolve immediately.@@ -551,4 +633,7 @@ You can also send a Broadcast message by making an HTTP request to Realtime serv```</TabPanel>+ <TabPanel id="python" label="Python">+ Unsupported in Python yet.+ </TabPanel></Tabs>diff --git a/apps/docs/content/guides/realtime/postgres-changes.mdx b/apps/docs/content/guides/realtime/postgres-changes.mdxindex 8ef18b661df..51a50672d38 100644--- a/apps/docs/content/guides/realtime/postgres-changes.mdx+++ b/apps/docs/content/guides/realtime/postgres-changes.mdx@@ -277,6 +277,18 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('schema-db-changes').on_postgres_changes(+ "*",+ schema="public",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -358,6 +370,18 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('schema-db-changes').on_postgres_changes(+ "INSERT", # Listen only to INSERTs+ schema="public",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -439,6 +463,18 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('schema-db-changes').on_postgres_changes(+ "UPDATE", # Listen only to UPDATEs+ schema="public",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -520,6 +556,18 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('schema-db-changes').on_postgres_changes(+ "DELETE", # Listen only to DELETEs+ schema="public",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -606,6 +654,19 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "UPDATE",+ schema="public",+ table="todos",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -693,6 +754,23 @@ val userChanges = myChannel.postgresChangeFlow<PostgresAction.Insert>(schema = "myChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "*",+ schema="public",+ table="messages"+ callback=lambda payload: print(payload)+).on_postgres_changes(+ "INSERT",+ schema="public",+ table="users",+ callback=lambda payload: print(payload)+).subscribe()+```+</TabPanel></Tabs>@@ -784,6 +862,20 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "INSERT",+ schema="public",+ table="todos",+ filter="id=eq.1",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -879,6 +971,20 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "UPDATE",+ schema="public",+ table="messages",+ filter="body=eq.hey",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -973,6 +1079,20 @@ supabase.realtime.connect()myChannel.join()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "INSERT",+ schema="public",+ table="messages",+ filter="body=neq.bye",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -1066,6 +1186,20 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "INSERT",+ schema="public",+ table="profiles",+ filter="age=lt.65",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -1159,6 +1293,20 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "UPDATE",+ schema="public",+ table="profiles",+ filter="age=lte.65",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -1252,6 +1400,20 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "UPDATE",+ schema="public",+ table="products",+ filter="quantity=gt.10",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -1345,6 +1507,20 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "UPDATE",+ schema="public",+ table="products",+ filter="quantity=gte.10",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -1438,6 +1614,20 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+changes = supabase.channel('db-changes').on_postgres_changes(+ "UPDATE",+ schema="public",+ table="products",+ filter="name=in.(red, blue, yellow)",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -1587,6 +1777,22 @@ changesmyChannel.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+supabase.realtime.set_auth('your-custom-jwt')++changes = supabase.channel('db-changes').on_postgres_changes(+ "UPDATE",+ schema="public",+ table="products",+ filter="name=in.(red, blue, yellow)",+ callback=lambda payload: print(payload)+)+.subscribe()+```+</TabPanel></Tabs>@@ -1634,6 +1840,13 @@ In Kotlin, you have to update the token manually per channel:myChannel.updateAuth("fresh-token")```+</TabPanel>+<TabPanel id="python" label="Python">++```python+supabase.realtime.set_auth('fresh-token')+```+</TabPanel></Tabs>diff --git a/apps/docs/content/guides/realtime/presence.mdx b/apps/docs/content/guides/realtime/presence.mdxindex 02c6cf182ad..3fc1ba375e1 100644--- a/apps/docs/content/guides/realtime/presence.mdx+++ b/apps/docs/content/guides/realtime/presence.mdx@@ -70,6 +70,34 @@ val supabase = createSupabaseClient(supabaseUrl, supabaseKey) {}```+</TabPanel>+<TabPanel id="python" label="Python">++```python+from supabase import create_client++SUPABASE_URL = 'https://<project>.supabase.co'+SUPABASE_KEY = '<your-anon-key>'++supabase = create_client(SUPABASE_URL, SUPABASE_KEY)+```++</TabPanel>+<TabPanel id="dart" label="Dart">++```dart+void main() {+ Supabase.initialize(+ url: 'https://<project>.supabase.co',+ anonKey: '<your-anon-key>',+ );++ runApp(MyApp());+}++final supabase = Supabase.instance.client;+```+</TabPanel></Tabs>@@ -156,6 +184,21 @@ presenceFlowroomOne.subscribe()```+</TabPanel>+<TabPanel id="python" label="Python">++Listen to the `sync`, `join`, and `leave` events triggered whenever any client joins or leaves the channel or changes their slice of state:++```python+room_one = supabase.channel('room_01')++room_one+ .on_presence_sync(lambda: print('sync', room_one.presenceState()))+ .on_presence_join(lambda key, curr_presences, joined_presences: print('join', key, curr_presences, joined_presences))+ .on_presence_leave(lambda key, curr_presences, left_presences: print('leave', key, curr_presences, left_presences))+ .subscribe()+```+</TabPanel></Tabs>@@ -249,6 +292,26 @@ roomOne.subscribe(blockUntilSubscribed = true) //You can also use the roomOne.stroomOne.track(userStatus)```+</TabPanel>+<TabPanel id="python" label="Python">++```python+room_one = supabase.channel('room_01')++user_status = {+ "user": 'user-1',+ "online_at": datetime.datetime.now().isoformat(),+}++def on_subscribe(status, err):+ if status != RealtimeSubscribeStates.SUBSCRIBED:+ return++ room_one.track(user_status)++room_one.subscribe(on_subscribe)+```+</TabPanel></Tabs>@@ -308,6 +371,13 @@ suspend fun untrackPresence() {untrackPresence()```+</TabPanel>+<TabPanel id="python" label="Python">++```python+room_one.untrack()+```+</TabPanel></Tabs>@@ -370,5 +440,18 @@ val channelC = supabase.channel("test") {}```+</TabPanel>+<TabPanel id="python" label="Python">++```python+channel_c = supabase.channel('test', {+ "config": {+ "presence": {+ "key": 'userId-123',+ },+ },+})+```+</TabPanel></Tabs>diff --git a/apps/docs/spec/supabase_py_v2.yml b/apps/docs/spec/supabase_py_v2.ymlindex 14176386b2c..6ee21432a82 100644--- a/apps/docs/spec/supabase_py_v2.yml+++ b/apps/docs/spec/supabase_py_v2.yml@@ -4409,6 +4409,222 @@ functions:)```+ - id: subscribe+ title: on().subscribe()+ notes: |+ - By default, Broadcast and Presence are enabled for all projects.+ - By default, listening to database changes is disabled for new projects due to database performance and security concerns. You can turn it on by managing Realtime's [replication](/docs/guides/api#realtime-api-overview).+ - You can receive the "previous" data for updates and deletes by setting the table's `REPLICA IDENTITY` to `FULL` (e.g., `ALTER TABLE your_table REPLICA IDENTITY FULL;`).+ - Row level security is not applied to delete statements. When RLS is enabled and replica identity is set to full, only the primary key is sent to clients.+ examples:+ - id: listen-to-broadcast+ name: Listen to broadcast messages+ isSpotlight: true+ code: |+ ```python+ channel = supabase.channel("room1")++ def on_subscribe(status, err):+ if status == RealtimeSubscribeStates.SUBSCRIBED:+ channel.send_broadcast('cursor-pos', { "x": random.random(), "y": random.random() })++ def handle_broadcast(payload):+ print("Cursor position received!", payload)++ channel.on_broadcast(event="cursor-pos", callback=handle_broadcast).subscribe(on_subscribe)+ ```+ - id: listen-to-presence-sync+ name: Listen to presence sync+ isSpotlight: true+ code: |+ ```python+ channel = supabase.channel('room1')++ def on_subscribe(status, err):+ if status == RealtimeSubscribeStates.SUBSCRIBED:+ channel.track({ "online_at": datetime.datetime.now().isoformat() })++ def handle_presence_sync():+ print('Synced presence state: ', channel.presence.state)++ channel.on_presence_sync(callback=handle_presence_sync).subscribe(on_subscribe)+ ```+ - id: listen-to-presence-join+ name: Listen to presence join+ isSpotlight: true+ code: |+ ```python+ channel = supabase.channel('room1')++ def handle_presence_join(key, current_presence, new_presence):+ print('Newly joined presences: ', new_presence)++ def on_subscribe(status, err):+ if status == RealtimeSubscribeStates.SUBSCRIBED:+ channel.track({ "online_at": datetime.datetime.now().isoformat() })++ channel.on_presence_join(callback=handle_presence_join).subscribe(on_subscribe)+ ```+ - id: listen-to-presence-leave+ name: Listen to presence leave+ isSpotlight: true+ code: |+ ```python+ channel = supabase.channel('room1')++ def handle_presence_leave(key, current_presence, left_presence):+ print('Newly left presences: ', left_presence)++ def on_subscribe(status, err):+ if status == RealtimeSubscribeStates.SUBSCRIBED:+ channel.track({ "online_at": datetime.datetime.now().isoformat() })+ channel.untrack()++ channel.on_presence_leave(callback=handle_presence_leave).subscribe(on_subscribe)+ ```+ - id: listen-to-all-database-changes+ name: Listen to all database changes+ isSpotlight: true+ code: |+ ```python+ supabase+ .channel('room1')+ .on_postgres_changes("*", schema="*", callback=handle_record_updated)+ .subscribe()+ ```+ - id: listen-to-a-specific-table+ name: Listen to a specific table+ code: |+ ```python+ supabase+ .channel('room1')+ .on_postgres_changes("*", schema="public", table="countries", callback=handle_record_updated)+ .subscribe()+ ```+ - id: listen-to-inserts+ name: Listen to inserts+ code: |+ ```python+ supabase+ .channel('room1')+ .on_postgres_changes("INSERT", schema="public", table="countries", callback=handle_record_inserted)+ .subscribe()+ ```+ - id: listen-to-updates+ name: Listen to updates+ description: |+ By default, Supabase will send only the updated record. If you want to receive the previous values as well you can+ enable full replication for the table you are listening to:++ ```sql+ alter table "your_table" replica identity full;+ ```+ code: |+ ```python+ supabase+ .channel('room1')+ .on_postgres_changes("UPDATE", schema="public", table="countries", callback=handle_record_updated)+ .subscribe()+ ```+ - id: listen-to-deletes+ name: Listen to deletes+ description: |+ By default, Supabase does not send deleted records. If you want to receive the deleted record you can+ enable full replication for the table you are listening too:++ ```sql+ alter table "your_table" replica identity full;+ ```+ code: |+ ```python+ supabase+ .channel('room1')+ .on_postgres_changes("DELETE", schema="public", table="countries", callback=handle_record_deleted)+ .subscribe()+ ```+ - id: listen-to-multiple-events+ name: Listen to multiple events+ description: You can chain listeners if you want to listen to multiple events for each table.+ code: |+ ```python+ supabase+ .channel('room1')+ .on_postgres_changes("INSERT", schema="public", table="countries", callback=handle_record_inserted)+ .on_postgres_changes("DELETE", schema="public", table="countries", callback=handle_record_deleted)+ .subscribe()+ ```+ - id: listening-to-row-level-changes+ name: Listen to row level changes+ description: You can listen to individual rows using the format `{table}:{col}=eq.{val}` - where `{col}` is the column name, and `{val}` is the value which you want to match.+ notes: |+ - ``eq`` filter works with all database types as under the hood, it's casting both the filter value and the database value to the correct type and then comparing them.+ code: |+ ```python+ supabase+ .channel('room1')+ .on_postgres_changes("UPDATE", schema="public", table="countries", filter="id=eq.200", callback=handle_record_updated)+ .subscribe()+ ```+ - id: broadcast-message+ title: broadcastMessage()+ description: |+ Broadcast a message to all connected clients to a channel.+ examples:+ - id: send-a-message+ name: Send a message via websocket+ isSpotlight: true+ code: |+ ```python+ channel = supabase.channel('room1')++ def on_subscribe(status, err):+ if status == RealtimeSubscribeStates.SUBSCRIBED:+ channel.send_broadcast('cursor-pos', { "x": random.random(), "y": random.random() })++ channel.subscribe(on_subscribe)+ ```+ response: |+ ```+ ok | timed out | error+ ```+ - id: get-channels+ title: getChannels()+ examples:+ - id: get-all-channels+ name: Get all channels+ isSpotlight: true+ code: |+ ```python+ channels = supabase.get_channels()+ ```++ - id: remove-channel+ title: removeChannel()+ notes: |+ - Removing a channel is a great way to maintain the performance of your project's Realtime service as well as your database if you're listening to Postgres changes. Supabase will automatically handle cleanup 30 seconds after a client is disconnected, but unused channels may cause degradation as more clients are simultaneously subscribed.+ examples:+ - id: removes-a-channel+ name: Removes a channel+ isSpotlight: true+ code: |+ ```python+ supabase.remove_channel(myChannel)+ ```++ - id: remove-all-channels+ title: removeAllChannels()+ notes: |+ - Removing channels is a great way to maintain the performance of your project's Realtime service as well as your database if you're listening to Postgres changes. Supabase will automatically handle cleanup 30 seconds after a client is disconnected, but unused channels may cause degradation as more clients are simultaneously subscribed.+ examples:+ - id: remove-all-channels+ name: Remove all channels+ isSpotlight: true+ code: |+ ```python+ supabase.remove_all_channels()+ ```++- id: list-bucketstitle: 'list_buckets()'notes: |