订阅数据库变化
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
实时授权 是接收广播消息所必需的。这是一个允许经过身份验证的用户收听主题消息的策略示例:
1create policy "Authenticated users can receive broadcasts"2on "realtime"."messages"3for select4to authenticated5using ( 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.
1create or replace function public.your_table_changes()2returns trigger3security definer4language plpgsql5as $$6begin7 perform realtime.broadcast_changes(8 '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 name9 TG_OP, -- event - the event that triggered the function10 TG_OP, -- operation - the operation that triggered the function11 TG_TABLE_NAME, -- table - the table that caused the trigger12 TG_TABLE_SCHEMA, -- schema - the schema of the table that caused the trigger13 NEW, -- new record - the record after the change14 OLD -- old record - the record before the change15 );16 return null;17end;18$$;创建一个触发器 #
🌐 Create a trigger
设置一个触发器,让函数在表格有任何更改后运行。
🌐 Set up a trigger so the function runs after any changes to the table.
1create trigger handle_your_table_changes2after insert or update or delete3on public.your_table4for each row5execute 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.
1import { createClient } from '@supabase/supabase-js'23const supabase = createClient('your_project_url', 'your_supabase_api_key')45// ---cut---6const gameId = 'id'7await supabase.realtime.setAuth() // Needed for Realtime Authorization8const changes = supabase9 .channel(`topic:${gameId}`, {10 config: { private: true },11 })12 .on('broadcast', { event: 'INSERT' }, (payload) => console.log(payload))13 .on('broadcast', { event: 'UPDATE' }, (payload) => console.log(payload))14 .on('broadcast', { event: 'DELETE' }, (payload) => console.log(payload))15 .subscribe()使用 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:
1begin;23-- remove the supabase_realtime publication4drop5 publication if exists supabase_realtime;67-- re-create the supabase_realtime publication with no tables8create publication supabase_realtime;910commit;1112-- add a table called 'messages' to the publication13-- (update this to match your tables)14alter15 publication supabase_realtime add table messages;流式插入 #
🌐 Streaming inserts
你可以使用 INSERT 事件来实时传输所有新行。
🌐 You can use the INSERT event to stream all new rows.
1// @noImplicitAny: false2import { createClient } from '@supabase/supabase-js'34const supabase = createClient('your_project_url', 'your_supabase_api_key')56// ---cut---7const channel = supabase8 .channel('schema-db-changes')9 .on(10 'postgres_changes',11 {12 event: 'INSERT',13 schema: 'public',14 },15 (payload) => console.log(payload)16 )17 .subscribe()流媒体更新 #
🌐 Streaming updates
你可以使用 UPDATE 事件来流式传输所有更新的行。
🌐 You can use the UPDATE event to stream all updated rows.
1// @noImplicitAny: false2import { createClient } from '@supabase/supabase-js'34const supabase = createClient('your_project_url', 'your_supabase_api_key')56// ---cut---7const channel = supabase8 .channel('schema-db-changes')9 .on(10 'postgres_changes',11 {12 event: 'UPDATE',13 schema: 'public',14 },15 (payload) => console.log(payload)16 )17 .subscribe()