Skip to content

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

guide 2

Iniane8e493f4413d
diff --git a/apps/reference/docs/guides/realtime/broadcast-presence.mdx b/apps/reference/docs/guides/realtime/broadcast.mdx
similarity index 81%
rename from apps/reference/docs/guides/realtime/broadcast-presence.mdx
rename to apps/reference/docs/guides/realtime/broadcast.mdx
index a29efeaa99e..3be23c885aa 100644
--- a/apps/reference/docs/guides/realtime/broadcast-presence.mdx
+++ b/apps/reference/docs/guides/realtime/broadcast.mdx
@@ -1,10 +1,12 @@
---
id: broadcast
-title: Supabase Broadcast
+title: Broadcast
description: Getting started with Realtime's broadcast feature
---
-Supabase Broadcast allows a client to broadcast messages and multiple receivers to receive the broadcasted message. The broadcasted messages are ephemeral, they are not persisted to the database and are directly relayed by the Realtime server.
+Supabase Broadcast allows a client to broadcast messages and multiple receivers to receive the broadcasted message. The broadcasted messages are ephemeral, they are not persisted to the database and are directly relayed by the Realtime server. This is ideal for broadcasting information like mouse moments where latency is important, but need not be persisted.
+
+The broadcast JS client methods are supported from supabase-js v2 release candidate.
To broadcast a message:
@@ -18,7 +20,7 @@ const supabase = createClient(
// The name of the channel can be set to any string.
// Set the same name across both the broadcasting and receiving clients.
-const channel = supabase.channel('broadcastChannel', {
+const channel = supabase.channel('user1-location', {
configs: {
broadcast: { ack: true },
},
@@ -53,7 +55,7 @@ const supabase = createClient(
)
// use the same channel name as the client broadcasting the message is using
-const channel = supabase.channel('broadcastChannel')
+const channel = supabase.channel('user1-location')
// listen to broadcasts
channel
diff --git a/apps/reference/docs/guides/realtime/presence.mdx b/apps/reference/docs/guides/realtime/presence.mdx
new file mode 100644
index 00000000000..ee3c96eedf9
--- /dev/null
+++ b/apps/reference/docs/guides/realtime/presence.mdx
@@ -0,0 +1,46 @@
+---
+id: presence
+title: Presence
+description: Getting started with Realtime's presence feature
+---
+
+Supabase Presence stores and synchronize shared state across clients. Presence is very similar to Broadcast, except that the state is “persisted” in the Channel so that new joiners can immediately get the state without waiting for another user to broadcast a message.
+
+The `sync` event is triggered whenever the shared state changes. The `join` event is triggered when a new client joins the channel and `leave` when a client leaves.
+
+Each client can use the `track` method to store an object in the shared state.
+
+The presence JS client methods are supported from supabase-js v2 release candidate.
+
+```js
+const { createClient } = require('@supabase/supabase-js')
+
+const supabase = createClient(
+ process.env.SUPABASE_URL,
+ process.env.SUPABASE_KEY
+)
+
+const channel = supabase.channel('online-users')
+
+function getRandomUser() {
+ const users = ['Alice', 'Bob', 'Mallory', 'Inian']
+ return users[Math.floor(Math.random() * users.length)]
+}
+
+channel
+ .on('presence', { event: 'sync' }, () => {
+ console.log('currently online users', channel.presenceState())
+ })
+ .on('presence', { event: 'join' }, ({ newPresences }) => {
+ console.log('a new user has joined', newPresences)
+ })
+ .on('presence', { event: 'leave' }, ({ leftPresences }) =>
+ console.log('a user has left', leftPresences)
+ )
+ .subscribe(async (status) => {
+ if (status === 'SUBSCRIBED') {
+ const status = await channel.track({ user_name: getRandomUser() })
+ console.log(status)
+ }
+ })
+```
diff --git a/apps/reference/nav/_referenceSidebars.js b/apps/reference/nav/_referenceSidebars.js
index d4cbae1315f..74ef6ca7783 100644
--- a/apps/reference/nav/_referenceSidebars.js
+++ b/apps/reference/nav/_referenceSidebars.js
@@ -84,7 +84,7 @@ const sidebars = {
type: 'category',
label: 'Realtime',
collapsed: true,
- items: ['guides/realtime/broadcast'],
+ items: ['guides/realtime/broadcast', 'guides/realtime/presence'],
},
{
type: 'category',