订阅数据库变化
Listen to database changes in real-time from your website or application.
你可以使用 Supabase 订阅实时数据库的变化。有两种可用的选项:
🌐 You can use Supabase to subscribe to real-time database changes. There are two options available:
- 广播。这是推荐的可扩展性和安全性的方法。
- Postgres 变更。这是一个更简单的方法。设置要求较少,但扩展性不如 Broadcast。
使用广播 #
🌐 Using Broadcast
要在记录被创建、更新或删除时自动发送消息,我们可以把一个 Postgres 触发器 附加到任意表。Supabase Realtime 提供了一个 realtime.broadcast_changes() 函数,我们可以结合触发器来使用。这个函数会使用一个私有通道,并且需要满足广播授权的 RLS 策略。
🌐 To automatically send messages when a record is created, updated, or deleted, we can attach a Postgres trigger to any table. Supabase Realtime provides a realtime.broadcast_changes() function which we can use in conjunction with a trigger. This function will use a private channel and needs broadcast authorization RLS policies to be met.
播放授权 #
🌐 Broadcast authorization
实时授权 是接收广播消息所必需的。这是一个允许经过身份验证的用户收听主题消息的策略示例:
create policy "Authenticated users can receive broadcasts"on "realtime"."messages"for selectto authenticatedusing ( true );创建触发器函数 #
🌐 Create a trigger function
创建一个函数,每当记录被创建、更新或删除时调用。这个函数将使用 Postgres 的一些原生 触发器变量。在这个例子中,我们希望有一个名为 topic:<record id> 的主题,我们将向其广播事件。
🌐 Create a function to call whenever a record is created, updated, or deleted. This function will make use of some of Postgres's native trigger variables. For this example, we want to have a topic with the name topic:<record id> to which we're going to broadcast events.
create or replace function public.your_table_changes()returns triggersecurity definerlanguage plpgsqlas $$begin perform realtime.broadcast_changes( 'topic:' || coalesce(NEW.id, OLD.id) ::text, -- topic - the topic to which you're broadcasting where you can use the topic id to build the topic name TG_OP, -- event - the event that triggered the function TG_OP, -- operation - the operation that triggered the function TG_TABLE_NAME, -- table - the table that caused the trigger TG_TABLE_SCHEMA, -- schema - the schema of the table that caused the trigger NEW, -- new record - the record after the change OLD -- old record - the record before the change ); return null;end;$$;创建一个触发器 #
🌐 Create a trigger
设置一个触发器,让函数在表格有任何更改后运行。
🌐 Set up a trigger so the function runs after any changes to the table.
create trigger handle_your_table_changesafter insert or update or deleteon public.your_tablefor each rowexecute function your_table_changes ();在客户端监听 #
🌐 Listening on client side
最后,在客户端,监听主题 topic:<record_id> 来接收事件。记得将通道设置为私有通道,因为 realtime.broadcast_changes 使用实时授权。
🌐 Finally, on the client side, listen to the topic topic:<record_id> to receive the events. Remember to set the channel as a private channel, since realtime.broadcast_changes uses Realtime Authorization.
const = 'id'await ..() // Needed for Realtime Authorizationconst = .(`topic:${}`, { : { : true }, }) .('broadcast', { : 'INSERT' }, () => .()) .('broadcast', { : 'UPDATE' }, () => .()) .('broadcast', { : 'DELETE' }, () => .()) .()使用 Postgres 的更改 #
🌐 Using Postgres Changes
Postgres 变更只需要最少的设置,但随着你的应用扩展,会有一些限制。我们建议在大多数情况下使用 Broadcast。
🌐 Postgres Changes require minimal setup, but have some limitations as your application scales. We recommend using Broadcast for most use cases.
启用 Postgres 更改 #
🌐 Enable Postgres Changes
你首先需要创建一个 supabase_realtime 发布,并将你想订阅的表添加到该发布中:
🌐 You'll first need to create a supabase_realtime publication and add your tables (that you want to subscribe to) to the publication:
begin;-- remove the supabase_realtime publicationdrop publication if exists supabase_realtime;-- re-create the supabase_realtime publication with no tablescreate publication supabase_realtime;commit;-- add a table called 'messages' to the publication-- (update this to match your tables)alter publication supabase_realtime add table messages;流式插入 #
🌐 Streaming inserts
你可以使用 INSERT 事件来实时传输所有新行。
🌐 You can use the INSERT event to stream all new rows.
const = .('schema-db-changes') .( 'postgres_changes', { : 'INSERT', : 'public', }, () => .() ) .()流媒体更新 #
🌐 Streaming updates
你可以使用 UPDATE 事件来流式传输所有更新的行。
🌐 You can use the UPDATE event to stream all updated rows.
const = .('schema-db-changes') .( 'postgres_changes', { : 'UPDATE', : 'public', }, () => .() ) .()