|
| 1 | +<br /> |
| 2 | +<p align="center"> |
| 3 | + <a href="https://supabase.io"> |
| 4 | + <picture> |
| 5 | + <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/supabase/supabase/main/packages/common/assets/images/supabase-logo-wordmark--dark.svg"> |
| 6 | + <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/supabase/supabase/main/packages/common/assets/images/supabase-logo-wordmark--light.svg"> |
| 7 | + <img alt="Supabase Logo" width="300" src="https://raw.githubusercontent.com/supabase/supabase/main/packages/common/assets/images/logo-preview.jpg"> |
| 8 | + </picture> |
| 9 | + </a> |
| 10 | + |
| 11 | + <h1 align="center">Supabase Realtime Client</h1> |
| 12 | + |
| 13 | + <h3 align="center">Send ephemeral messages with <b>Broadcast</b>, track and synchronize state with <b>Presence</b>, and listen to database changes with <b>Postgres Change Data Capture (CDC)</b>.</h3> |
| 14 | + |
| 15 | + <p align="center"> |
| 16 | + <a href="https://supabase.com/docs/guides/realtime">Guides</a> |
| 17 | + · |
| 18 | + <a href="https://supabase.com/docs/reference/python">Reference Docs</a> |
| 19 | + · |
| 20 | + <a href="https://multiplayer.dev">Multiplayer Demo</a> |
| 21 | + </p> |
| 22 | +</p> |
| 23 | + |
| 24 | +# Overview |
| 25 | + |
| 26 | +This client enables you to use the following Supabase Realtime's features: |
| 27 | + |
| 28 | +- **Broadcast**: send ephemeral messages from client to clients with minimal latency. Use cases include sharing cursor positions between users. |
| 29 | +- **Presence**: track and synchronize shared state across clients with the help of CRDTs. Use cases include tracking which users are currently viewing a specific webpage. |
| 30 | +- **Postgres Change Data Capture (CDC)**: listen for changes in your PostgreSQL database and send them to clients. |
| 31 | + |
| 32 | +# Usage |
| 33 | + |
| 34 | +## Installing the Package |
| 35 | + |
| 36 | +```bash |
| 37 | +pip3 install realtime |
| 38 | +``` |
| 39 | + |
| 40 | +## Creating a Channel |
| 41 | + |
| 42 | +```python |
| 43 | +import asyncio |
| 44 | +from typing import Optional |
| 45 | + |
| 46 | +from realtime import AsyncRealtimeClient, RealtimeSubscribeStates |
| 47 | + |
| 48 | + |
| 49 | +async def main(): |
| 50 | + REALTIME_URL = "ws://localhost:4000/websocket" |
| 51 | + API_KEY = "1234567890" |
| 52 | + |
| 53 | + socket = AsyncRealtimeClient(REALTIME_URL, API_KEY) |
| 54 | + channel = socket.channel("test-channel") |
| 55 | + |
| 56 | + def _on_subscribe(status: RealtimeSubscribeStates, err: Optional[Exception]): |
| 57 | + if status == RealtimeSubscribeStates.SUBSCRIBED: |
| 58 | + print("Connected!") |
| 59 | + elif status == RealtimeSubscribeStates.CHANNEL_ERROR: |
| 60 | + print(f"There was an error subscribing to channel: {err.args}") |
| 61 | + elif status == RealtimeSubscribeStates.TIMED_OUT: |
| 62 | + print("Realtime server did not respond in time.") |
| 63 | + elif status == RealtimeSubscribeStates.CLOSED: |
| 64 | + print("Realtime channel was unexpectedly closed.") |
| 65 | + |
| 66 | + await channel.subscribe(_on_subscribe) |
| 67 | +``` |
| 68 | + |
| 69 | +### Notes: |
| 70 | + |
| 71 | +- `REALTIME_URL` is `ws://localhost:4000/socket` when developing locally and `wss://<project_ref>.supabase.co/realtime/v1` when connecting to your Supabase project. |
| 72 | +- `API_KEY` is a JWT whose claims must contain `exp` and `role` (existing database role). |
| 73 | +- Channel name can be any `string`. |
| 74 | + |
| 75 | +## Broadcast |
| 76 | + |
| 77 | +Your client can send and receive messages based on the `event`. |
| 78 | + |
| 79 | +```python |
| 80 | +# Setup... |
| 81 | + |
| 82 | +channel = client.channel( |
| 83 | + "broadcast-test", {"config": {"broadcast": {"ack": False, "self": False}}} |
| 84 | +) |
| 85 | + |
| 86 | +await channel.on_broadcast("some-event", lambda payload: print(payload)).subscribe() |
| 87 | +await channel.send_broadcast("some-event", {"hello": "world"}) |
| 88 | +``` |
| 89 | + |
| 90 | +### Notes: |
| 91 | + |
| 92 | +- Setting `ack` to `true` means that the `channel.send` promise will resolve once server replies with acknowledgement that it received the broadcast message request. |
| 93 | +- Setting `self` to `true` means that the client will receive the broadcast message it sent out. |
| 94 | +- Setting `private` to `true` means that the client will use RLS to determine if the user can connect or not to a given channel. |
| 95 | + |
| 96 | +## Presence |
| 97 | + |
| 98 | +Your client can track and sync state that's stored in the channel. |
| 99 | + |
| 100 | +```python |
| 101 | +# Setup... |
| 102 | + |
| 103 | +channel = client.channel( |
| 104 | + "presence-test", |
| 105 | + { |
| 106 | + "config": { |
| 107 | + "presence": { |
| 108 | + "key": "" |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +) |
| 113 | + |
| 114 | +channel.on_presence_sync(lambda: print("Online users: ", channel.presence_state())) |
| 115 | +channel.on_presence_join(lambda new_presences: print("New users have joined: ", new_presences)) |
| 116 | +channel.on_presence_leave(lambda left_presences: print("Users have left: ", left_presences)) |
| 117 | + |
| 118 | +await channel.track({ 'user_id': 1 }) |
| 119 | +``` |
| 120 | + |
| 121 | +## Postgres CDC |
| 122 | + |
| 123 | +Receive database changes on the client. |
| 124 | + |
| 125 | +```python |
| 126 | +# Setup... |
| 127 | + |
| 128 | +channel = client.channel("db-changes") |
| 129 | + |
| 130 | +channel.on_postgres_changes( |
| 131 | + "*", |
| 132 | + schema="public", |
| 133 | + callback=lambda payload: print("All changes in public schema: ", payload), |
| 134 | +) |
| 135 | + |
| 136 | +channel.on_postgres_changes( |
| 137 | + "INSERT", |
| 138 | + schema="public", |
| 139 | + table="messages", |
| 140 | + callback=lambda payload: print("All inserts in messages table: ", payload), |
| 141 | +) |
| 142 | + |
| 143 | +channel.on_postgres_changes( |
| 144 | + "UPDATE", |
| 145 | + schema="public", |
| 146 | + table="users", |
| 147 | + filter="username=eq.Realtime", |
| 148 | + callback=lambda payload: print( |
| 149 | + "All updates on users table when username is Realtime: ", payload |
| 150 | + ), |
| 151 | +) |
| 152 | + |
| 153 | +channel.subscribe( |
| 154 | + lambda status, err: status == RealtimeSubscribeStates.SUBSCRIBED |
| 155 | + and print("Ready to receive database changes!") |
| 156 | +) |
| 157 | +``` |
| 158 | + |
| 159 | +## Get All Channels |
| 160 | + |
| 161 | +You can see all the channels that your client has instantiated. |
| 162 | + |
| 163 | +```python |
| 164 | +# Setup... |
| 165 | + |
| 166 | +client.get_channels() |
| 167 | +``` |
| 168 | + |
| 169 | +## Cleanup |
| 170 | + |
| 171 | +It is highly recommended that you clean up your channels after you're done with them. |
| 172 | + |
| 173 | +- Remove a single channel |
| 174 | + |
| 175 | +```python |
| 176 | +# Setup... |
| 177 | + |
| 178 | +channel = client.channel('some-channel-to-remove') |
| 179 | + |
| 180 | +channel.subscribe() |
| 181 | + |
| 182 | +await client.remove_channel(channel) |
| 183 | +``` |
| 184 | + |
| 185 | +- Remove all channels |
| 186 | + |
| 187 | +```python |
| 188 | +# Setup... |
| 189 | + |
| 190 | +channel1 = client.channel('a-channel-to-remove') |
| 191 | +channel2 = client.channel('another-channel-to-remove') |
| 192 | + |
| 193 | +await channel1.subscribe() |
| 194 | +await channel2.subscribe() |
| 195 | + |
| 196 | +await client.remove_all_channels() |
| 197 | +``` |
| 198 | + |
| 199 | +## Credits |
| 200 | + |
| 201 | +This repo draws heavily from [phoenix-js](https://github.com/phoenixframework/phoenix/tree/master/assets/js/phoenix). |
| 202 | + |
| 203 | +## License |
| 204 | + |
| 205 | +MIT. |
0 commit comments