The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
convert Broadcast to full guide
Copple869369b2a3f6
diff --git a/apps/docs/pages/guides/realtime/broadcast.mdx b/apps/docs/pages/guides/realtime/broadcast.mdxindex 04496b4f52c..37c4d263ed3 100644--- a/apps/docs/pages/guides/realtime/broadcast.mdx+++ b/apps/docs/pages/guides/realtime/broadcast.mdx@@ -1,191 +1,80 @@import Layout from '~/layouts/DefaultGuideLayout'-import StepHikeCompact from '~/components/StepHikeCompact'export const meta = {title: 'Broadcast',- subtitle: "Get up and running with Realtime's Broadcast feature",- breadcrumb: 'Realtime Broadcast Quickstart',+ subtitle: 'Send and receive messages using Realtime Broadcast',+ description: 'Send and receive messages using Realtime Broadcast',+ // breadcrumb: 'Realtime Broadcast Quickstart',}-Realtime Broadcast follows the [publish-subscribe pattern](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) where a client publishes messages to a channel based on a unique topic. For example, a user could send a message to a channel with topic `room-1`.+Let's explore how to implement Realtime Broadcast to send messages between clients.-Other clients can receive the message in real-time by subscribing to the channel with topic `room-1`. These clients can continue to receive messages as long as they continue to be online and subscribed to the same channel topic.+## Usage-An example use-case is sharing a user's cursor position with other clients in an online tool or game.+You can use the Supabase client libraries to send and receive Broadcast messages.-## Quick start+### Initialize the client-Let's explore how to implement Realtime Broadcast so you can integrate it into your use case.+Go to your Supabase project's [API Settings](https://supabase.com/dashboard/project/_/settings/api) and grab the `URL` and `anon` public API key.-<StepHikeCompact>-- <StepHikeCompact.Step step={1}>-- <StepHikeCompact.Details title="Install the client">-- Install the Supabase JavaScript client.-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```bash- npm install @supabase/supabase-js- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={2}>-- <StepHikeCompact.Details title="Create the first client">-- This client will be used to listen for messages.-- Go to your Supabase project's [API Settings](https://supabase.com/dashboard/project/_/settings/api) and grab the `URL` and `anon` public API key.-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```js- import {- createClient- } from '@supabase/supabase-js'-- const clientA = createClient(- 'https://<project>.supabase.co',- '<your-anon-key>'- )- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={3}>-- <StepHikeCompact.Details title="Create a channel">-- A channel's topic can be anything except for `'realtime'`.-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```js- const channelA = clientA.channel('room-1')- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={4}>-- <StepHikeCompact.Details title="Listen for messages">-- Specify the Broadcast event you want the `on` handler to listen for. This event name can be anything you want. We'll send a broadcast message with this event name later on.-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```js- channelA- .on(- 'broadcast',- { event: 'test' },- (payload) => console.log(payload)- )- .subscribe()- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={5}>-- <StepHikeCompact.Details title="Create the second client">-- This client will be used to send a message.-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```js- const clientB = createClient(- 'https://<project>.supabase.co',- '<your-anon-key>'- )- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={6}>-- <StepHikeCompact.Details title="Create another channel">-- This channel's topic must match `channelA`'s.-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```js- const channelB = clientB.channel('room-1')- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>+```js+import { createClient } from '@supabase/supabase-js'- <StepHikeCompact.Step step={7}>-- <StepHikeCompact.Details title="Send message">+const SUPABASE_URL = 'https://<project>.supabase.co'+const SUPABASE_KEY = '<your-anon-key>'- Subscribe to channel and send a message.+const client = createClient(SUPABASE_URL, SUPABASE_KEY)+```- The payload's `event` must match channelA's `event` in the `on` handler.+### Listening to Broadcast messages- </StepHikeCompact.Details>+You can provide a callback for the `broadcast` channel to receive message. In this example we will receive any `broadcast` messages in `room-1`:- <StepHikeCompact.Code>+{/* prettier-ignore */}+```js+// Join a room/topic. Can be anything except for 'realtime'.+const channelA = clientA.channel('room-1')- ```js- channelB.subscribe((status) => {- if (status === 'SUBSCRIBED') {- channelB.send({- type: 'broadcast',- event: 'test',- payload: {- message: 'hello, world'- },- })- }- })- ```+// Simple function to log any messages we receive+function messageReceived(payload) {+ console.log(payload)+}- </StepHikeCompact.Code>+// Subscribe to the Channel+channelA+ .on(+ 'broadcast',+ { event: 'test' },+ (payload) => messageReceived(payload)+ )+ .subscribe()+```- </StepHikeCompact.Step>+### Sending Broadcast messages- <StepHikeCompact.Step step={8}>-- <StepHikeCompact.Details title="First client receives message">+We can send Broadcast messages using `channelB.send()`. Let's set up another client to send messages.- `clientA` receives the message `clientB` sent.+{/* prettier-ignore */}+```js+// Join a room/topic. Can be anything except for 'realtime'.+const channelB = clientA.channel('room-1')- </StepHikeCompact.Details>+channelB.subscribe((status) => {+ // Wait for successful connection+ if (status !== 'SUBSCRIBED') {+ return null+ }- </StepHikeCompact.Step>+ // Send a message once the client is subscribed+ channelB.send({+ type: 'broadcast',+ event: 'test',+ payload: { message: 'hello, world' },+ })+})+```-</StepHikeCompact>+Before sending messages we need to ensure the client is connected, which we have done within the `subscribe()` callback.## Broadcast options@@ -193,27 +82,29 @@ There are additional Broadcast functionality that you can enable when creating a### Self-send messages-You can have a client broadcast a message and then receive the same message by setting Broadcast's `self` config to `true`. Without this, broadcast messages are only sent to other clients.+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`.+{/* prettier-ignore */}```js-const channelC = clientC.channel('room-2', {+const myChannel = supabase.channel('room-2', {config: {- broadcast: {- self: true,- },+ broadcast: { self: true },},})-channelC.on('broadcast', { event: 'test-my-messages' }, (payload) => console.log(payload))--channelC.subscribe((status) => {- if (status === 'SUBSCRIBED') {- channelC.send({- type: 'broadcast',- event: 'test-my-messages',- payload: { message: 'talking to myself' },- })- }+myChannel.on(+ 'broadcast',+ { event: 'test-my-messages' },+ (payload) => console.log(payload)+)++myChannel.subscribe((status) => {+ if (status !== 'SUBSCRIBED') { return }+ channelC.send({+ type: 'broadcast',+ event: 'test-my-messages',+ payload: { message: 'talking to myself' },+ })})```@@ -221,35 +112,29 @@ channelC.subscribe((status) => {You can confirm that Realtime received your message by setting Broadcast's `ack` config to `true`.+{/* prettier-ignore */}```js-const channelD = clientD.channel('room-3', {+const myChannel = clientD.channel('room-3', {config: {- broadcast: {- ack: true,- },+ broadcast: { ack: true },},})-channelD.subscribe(async (status) => {- if (status === 'SUBSCRIBED') {- const resp = await channelD.send({- type: 'broadcast',- event: 'acknowledge',- payload: {},- })+myChannel.subscribe(async (status) => {+ if (status !== 'SUBSCRIBED') { return }- console.log('resp', resp)- }+ const serverResponse = await myChannel.send({+ type: 'broadcast',+ event: 'acknowledge',+ payload: {},+ })++ console.log('serverResponse', serverResponse)})```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.-## More Realtime Quickstarts--- [Presence Quickstart](/docs/guides/realtime/presence)-- [Postgres Changes Quickstart](/docs/guides/realtime/postgres-changes)-export const Page = ({ children }) => <Layout meta={meta} children={children} />export default Page