Skip to content

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

docs: add async/await JS example to message broadcasting (#31232)

Kevin Rockerd15ae6531610
diff --git a/apps/docs/content/guides/realtime/broadcast.mdx b/apps/docs/content/guides/realtime/broadcast.mdx
index 09291a4e6ea..57c94f8ab4a 100644
--- a/apps/docs/content/guides/realtime/broadcast.mdx
+++ b/apps/docs/content/guides/realtime/broadcast.mdx
@@ -224,6 +224,35 @@ You can provide a callback for the `broadcast` channel to receive message. This
})
```
+ </TabPanel>
+ <TabPanel id="js-async" label="Async JavaScript">
+
+ We can send Broadcast messages using `channelB.send()`. Let's set up another client to send messages.
+
+ We can wrap the subscription in a promise to use async/await.
+
+ {/* prettier-ignore */}
+ ```js
+ // Join a room/topic. Can be anything except for 'realtime'.
+ const channelB = supabase.channel('room-1')
+
+ const channelSubscribed = await new Promise((resolve, reject) => {
+ channelB.subscribe((status) => {
+ if (status === 'SUBSCRIBED') {
+ resolve(channel);
+ } else {
+ reject("Could not subscribe to channel");
+ }
+ })
+ });
+
+ channelSubscribed.send({
+ type: 'broadcast',
+ event: 'test',
+ payload: { message: 'hello, world' },
+ });
+ ```
+
</TabPanel>
<TabPanel id="dart" label="Dart">