Skip to content

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

extend concepts with more details

Copple248b3a42be8b
diff --git a/apps/docs/pages/guides/realtime/concepts.mdx b/apps/docs/pages/guides/realtime/concepts.mdx
index af4dee031bd..eae39be7c4d 100644
--- a/apps/docs/pages/guides/realtime/concepts.mdx
+++ b/apps/docs/pages/guides/realtime/concepts.mdx
@@ -4,10 +4,11 @@ export const meta = {
id: 'channels',
title: 'Realtime Concepts',
description: 'Learn about channels and other features in Supabase Realtime',
+ subtitle: 'Learn about channels and other features in Supabase Realtime',
sidebar_label: 'Concepts',
}
-Supabase Realtime lets you to build real-time applications with collaborative/multiplayer functionality. It includes 3 core features:
+You can use Supabase Realtime to build real-time applications with collaborative/multiplayer functionality. It includes 3 core features:
- [Broadcast](/docs/guides/realtime/broadcast): sends rapid, ephemeral messages to other connected clients. You can use it to track mouse movements, for example.
- [Presence](/docs/guides/realtime/presence): sends user state between connected clients. You can use it to show an "online" status, which disappears when a user is disconnected.
@@ -15,23 +16,78 @@ Supabase Realtime lets you to build real-time applications with collaborative/mu
## Channels
-When you initialize your Supabase Realtime client, you define a `topic` that uniquely references a channel. Everyone connected to the same Channel `topic` receives the same messages.
+A Channel is the basic building block of Realtime. You can think of a Channel as a chatroom, similar to a Discord or Slack channel, where participants are able to see who's online and send and receive messages.
+
+When you initialize your Supabase Realtime client, you define a `topic` that uniquely references a channel. Clients can bi-directionally send and receive messages over a Channel.
```js
import { createClient } from '@supabase/supabase-js'
const client = createClient('https://<project>.supabase.co', '<your-anon-key>')
-const channel = client.channel('my-topic') // set your topic here
+const roomOne = client.channel('room-one') // set your topic here
+```
+
+## Broadcast
+
+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-one`.
+
+```js
+roomOne.send({
+ type: 'broadcast',
+ event: 'test',
+ payload: { message: 'hello, world' },
+})
+```
+
+Other clients can receive the message in real-time by subscribing to the Channel with topic `room-one`. These clients continue to receive messages as long as they are subscribed and connected to the same Channel topic.
+
+An example use-case is sharing a user's cursor position with other clients in an online game.
+
+## Presence
+
+Presence can be used to share and invidual's state with others within a Channel.
+
+```js
+const presenceTrackStatus = await roomOne.track({
+ user: 'user-1',
+ online_at: new Date().toISOString(),
+})
```
-Clients can bi-directionally send and receive messages over a Channel. The Realtime backend can also push messages to all clients connected to the same Channel.
+Each client maintains their own state, and this is then combined into a "shared state" for that Channel topic. It's commonly used for sharing statuses (eg: "online" or "inactive"). The neat thing about Presence is that if a client is suddenly disconnected (for example, they go offline), their state is automatically removed from the shared state. If you've ever tried to build an “I'm online” feature which handles unexpected disconnects, you'll appreciate how useful this is.
-A single client can receive change records from Postgres, Broadcast messages from other clients, and Presence updates all over the same Channel.
+When a new client subscribes to a channel, it will immediately receive the channel's latest state in a single message because the state is held by the Realtime server.
## Choosing between Broadcast and Presence
-We recommend using Broadcast by default, and then Presence when required. Presence merges all changes into a shared state for every client connected to the same channel. If you use Presence, it's best to throttle your changes so that you are sending updates less frequently.
+We recommend using Broadcast by default, and then Presence when required. Presence utilizes an in-memory conflict-free replicated data type (CRDT) to track and synchronize shared state in an eventually consistent manner. It computes the difference between existing state and new state changes and sends the necessary updates to clients via Broadcast. This is computationally heavy, so you should use it sparingly. If you use Presence, it's best to throttle your changes so that you are sending updates less frequently.
+
+## Reatlime extensions
+
+Channels provide a generic networking solution. Supabase Realtime is designed to leverage this networking primitive with "extensions". We currently support one extension: Postgres changes.
+
+### Postgres changes
+
+The Postgres Changes extension listens for database changes and sends them to clients. Clients are required to subscribe with a JWT dictating which changes they are allowed to receive based on the database's [Row Level Security](/docs/guides/auth/row-level-security).
+
+```js
+const allChanges = client
+ .channel('schema-db-changes')
+ .on(
+ 'postgres_changes',
+ {
+ event: '*',
+ schema: 'public',
+ },
+ (payload) => console.log(payload)
+ )
+ .subscribe()
+```
+
+Anyone with access to a valid JWT signed with the project's JWT secret is able to listen to your database's changes, unless tables have [Row Level Security](/docs/guides/auth/row-level-security) enabled and policies in place.
+
+Clients can choose to receive `INSERT`, `UPDATE`, `DELETE`, or `*` (all) changes for all changes in a schema, a table in a schema, or a column's value in a table. Your clients should only listen to tables in the `public` schema and you must first enable the tables you want your clients to listen to.
export const Page = ({ children }) => <Layout meta={meta} children={children} />