Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update broadcast.mdx | add security definer to example (#33533)

TheOtherBrian1b36315e62c16
diff --git a/apps/docs/content/guides/realtime/broadcast.mdx b/apps/docs/content/guides/realtime/broadcast.mdx
index 38eb434e251..fb544f58e5f 100644
--- a/apps/docs/content/guides/realtime/broadcast.mdx
+++ b/apps/docs/content/guides/realtime/broadcast.mdx
@@ -691,3 +691,204 @@ You can pass configuration options while initializing the Supabase Client.
</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.
+
+### Send messages using REST calls
+
+You can also send a Broadcast message by making an HTTP request to Realtime servers. This is useful when you want to send messages from your server or client without having to first establish a WebSocket connection.
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+ queryGroup="language"
+>
+ <TabPanel id="js" label="JavaScript">
+ <Admonition type="note">
+
+ This is currently available only in the Supabase JavaScript client version 2.37.0 and later.
+
+ </Admonition>
+
+ ```js
+ const channel = supabase.channel('test-channel')
+
+ // No need to subscribe to channel
+
+ channel
+ .send({
+ type: 'broadcast',
+ event: 'test',
+ payload: { message: 'Hi' },
+ })
+ .then((resp) => console.log(resp))
+
+ // Remember to clean up the channel
+
+ supabase.removeChannel(channel)
+
+ ```
+
+ </TabPanel>
+ <TabPanel id="dart" label="Dart">
+ ```dart
+ // No need to subscribe to channel
+
+ final channel = supabase.channel('test-channel');
+ final res = await channel.sendBroadcastMessage(
+ event: "test",
+ payload: {
+ 'message': 'Hi',
+ },
+ );
+ print(res);
+ ```
+
+ </TabPanel>
+ <TabPanel id="swift" label="Swift">
+ ```swift
+ let myChannel = await supabase.channel("room-2") {
+ $0.broadcast.acknowledgeBroadcasts = true
+ }
+
+ // No need to subscribe to channel
+
+ await myChannel.broadcast(event: "test", message: ["message": "HI"])
+ ```
+
+ </TabPanel>
+ <TabPanel id="kotlin" label="Kotlin">
+ ```kotlin
+ val myChannel = supabase.channel("room-2") {
+ broadcast {
+ acknowledgeBroadcasts = true
+ }
+ }
+
+ // No need to subscribe to channel
+
+ myChannel.broadcast(event = "test", buildJsonObject {
+ put("message", "Hi")
+ })
+ ```
+
+ </TabPanel>
+ <TabPanel id="python" label="Python">
+ Unsupported in Python yet.
+ </TabPanel>
+</Tabs>
+
+## Trigger broadcast messages from your database
+
+<Admonition type="caution">
+
+This feature is currently in Public Alpha. If you have any issues [submit a support ticket](https://supabase.help).
+
+</Admonition>
+
+### How it works
+
+Broadcast Changes allows you to trigger messages from your database. To achieve it Realtime is directly reading your WAL (Write Append Log) file using a publication against the `realtime.messages` table so whenever a new insert happens a message is sent to connected users.
+
+It uses partitioned tables per day which allows the deletion your previous messages in a performant way by dropping the physical tables of this partitioned table. Tables older than 3 days old are deleted.
+
+Broadcasting from the database works like a client-side broadcast, using WebSockets to send JSON packages. [Realtime Authorization](/docs/guides/realtime/authorization) is required and enabled by default to protect your data.
+
+The database broadcast feature provides two functions to help you send messages:
+
+- `realtime.send` will insert a message into realtime.messages without a specific format.
+- `realtime.broadcast_changes` will insert a message with the required fields to emit database changes to clients. This helps you set up triggers on your tables to emit changes.
+
+### Broadcasting a message from your database
+
+The `realtime.send` function provides the most flexibility by allowing you to broadcast messages from your database without a specific format. This allows you to use database broadcast for messages that aren't necessarily tied to the shape of a Postgres row change.
+
+```sql
+SELECT realtime.send (
+ to_jsonb ('{}'::text), -- JSONB Payload
+ 'event', -- Event name
+ 'topic', -- Topic
+ FALSE -- Public / Private flag
+);
+```
+
+### Broadcast record changes
+
+#### Setup realtime authorization
+
+Realtime Authorization is required and enabled by default. To allow your users to listen to messages from topics, create a RLS (Row Level Security) policy:
+
+```sql
+CREATE POLICY "authenticated can receive broadcasts"
+ON "realtime"."messages"
+FOR SELECT
+TO authenticated
+USING ( true );
+
+```
+
+See the [Realtime Authorization](/docs/guides/realtime/authorization) docs to learn how to set up more specific policies.
+
+#### Set up trigger function
+
+First, set up a trigger function that uses `realtime.broadcast_changes` to insert an event whenever it is triggered. The event is set up to include data on the schema, table, operation, and field changes that triggered it.
+
+For this example use case, we want to have a topic with the name `topic:<record id>` to which we're going to broadcast events.
+
+```sql
+CREATE OR REPLACE FUNCTION public.your_table_changes()
+RETURNS trigger
+SECURITY DEFINER SET search_path = ''
+AS $$
+BEGIN
+ PERFORM realtime.broadcast_changes(
+ 'topic:' || NEW.id::text, -- topic
+ TG_OP, -- event
+ TG_OP, -- operation
+ TG_TABLE_NAME, -- table
+ TG_TABLE_SCHEMA, -- schema
+ NEW, -- new record
+ OLD -- old record
+ );
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql;
+```
+
+Of note are the Postgres native trigger special variables used:
+
+- `TG_OP` - the operation that triggered the function
+- `TG_TABLE_NAME` - the table that caused the trigger
+- `TG_TABLE_SCHEMA` - the schema of the table that caused the trigger invocation
+- `NEW` - the record after the change
+- `OLD` - the record before the change
+
+You can read more about them in this [guide](https://www.postgresql.org/docs/current/plpgsql-trigger.html#PLPGSQL-DML-TRIGGER).
+
+#### Set up trigger
+
+Next, set up a trigger so the function runs whenever your target table has a change.
+
+```sql
+CREATE TRIGGER broadcast_changes_for_your_table_trigger
+AFTER INSERT OR UPDATE OR DELETE ON public.your_table
+FOR EACH ROW
+EXECUTE FUNCTION your_table_changes ();
+```
+
+As you can see, it will be broadcasting all operations so our users will receive events when records are inserted, updated or deleted from `public.your_table` .
+
+#### Listen on client side
+
+Finally, client side will requires to be set up to listen to the topic `topic:<record id>` to receive the events.
+
+```jsx
+const gameId = 'id'
+await supabase.realtime.setAuth() // Needed for Realtime Authorization
+const changes = supabase
+ .channel(`topic:${gameId}`)
+ .on('broadcast', { event: 'INSERT' }, (payload) => console.log(payload))
+ .on('broadcast', { event: 'UPDATE' }, (payload) => console.log(payload))
+ .on('broadcast', { event: 'DELETE' }, (payload) => console.log(payload))
+ .subscribe()
+```