实时概念
概念 #
🌐 Concepts
有几个概念和术语有助于理解实时是怎么工作的。
🌐 There are several concepts and terminology that is useful to understand how Realtime works.
- 通道:Realtime 的基础。可以把它们想象成客户端可以交流和监听事件的房间。通道是由主题名称以及它们是公共还是私有来识别的。
- 主题:通道的名称。它们用于识别通道,是用于识别通道的字符串。
- 事件:可以发送和接收的信息类型。
- 有效负载:实际发送和接收的数据,用户将据此进行操作。
- 并发连接:所有客户端订阅的总通道数。
通道 #
🌐 Channels
通道是实时功能的基础。可以把它们想象成客户端可以交流和监听事件的房间。通道由一个主题名称来标识,并且分为公开或私密。
🌐 Channels are the foundation of Realtime. Think of them as rooms where clients can communicate and listen to events. Channels are identified by a topic name and if they are public or private.
对于私有通道,你需要使用 实时授权 来控制对通道的访问以及他们是否可以发送消息。对于公共通道,任何用户都可以订阅通道、发送和接收消息。
🌐 For private channels, you need to use Realtime Authorization to control access to the channel and if they are able to send messages. For public channels, any user can subscribe to the channel, send and receive messages.
你可以在实时设置中将你的项目设置为仅使用私密通道或同时使用私密和公共通道。
🌐 You can set your project to use only private channels or both private and public channels in the Realtime Settings.
如果你有一个私密通道和一个公开通道,它们的主题名称相同,Realtime 会把它们看作不同的通道,并且不会在它们之间发送消息。
🌐 If you have a private channel and a public channel with the same topic name, Realtime sees them as unique channels and won't send messages between them.
数据库资源 #
🌐 Database resources
数据库连接 #
🌐 Database connections
Realtime 使用多个数据库连接来执行各种操作。你可以通过 Realtime 设置 配置其中的一些连接。
🌐 Realtime uses several database connections to perform various operations. You can configure some of these connections through Realtime Settings.
连接包括:
🌐 The connections include:
- 迁移:两个临时连接,用于在需要时运行数据库迁移
- 授权:可配置的连接池,用于在加入时检查始终启动的授权策略。
- 从数据库广播:一个连接用于从复制槽接收数据,将更改广播给客户端,这个连接总是启动的。
- Postgres 变更:需要多个连接池。只有在使用 Postgres 变更时,这些连接池才会启动。
- 订阅管理:管理 PostgreSQL 变更的订阅者
- 订阅清理:清理 Postgres 变更的订阅者
- WAL 拉取:从数据库拉取更改
连接数量会根据你的计算附加组件的大小和配置而有所不同。下表显示了不同计算附加组件版本的默认连接池大小:
🌐 The number of connections varies based on your compute add-on size and configuration. The following table shows the default connection pool sizes for different compute add-on variants:
| 计算附加组件 | 从数据库广播 | 授权池大小 | 订阅管理 | 订阅清理 | WAL 拉取 |
|---|---|---|---|---|---|
| Nano | 1 | 2 | 2 | 2 | 2 |
| Micro | 1 | 2 | 2 | 2 | 2 |
| Small | 1 | 5 | 4 | 4 | 4 |
| Medium | 1 | 5 | 4 | 4 | 4 |
| Large | 1 | 5 | 4 | 4 | 4 |
| XL | 1 | 10 | 7 | 7 | 7 |
| 2XL | 1 | 10 | 7 | 7 | 7 |
| 4XL | 1 | 10 | 7 | 7 | 7 |
| 8XL | 1 | 15 | 9 | 9 | 9 |
| 12XL | 1 | 15 | 9 | 9 | 9 |
| 16XL | 1 | 15 | 9 | 9 | 9 |
| >16XL | 1 | 15 | 9 | 9 | 9 |
你可以通过 Realtime 配置中的 Database connection pool size 参数自定义 Authorization Pool Size。如果没有指定,将使用表中显示的默认值。
🌐 You can customize Authorization Pool Size through the Database connection pool size parameter in your Realtime configuration. If not specified, the default values shown in the table will be used.
复制槽 #
🌐 Replication slots
实时模式最多也只使用2个复制槽。
🌐 Realtime also uses, at maximum, 2 replication slots.
- 从数据库广播:将数据库的更改广播给客户端
- Postgres 变更:监听数据库的变更
架构和表 #
🌐 Schema and tables
realtime 架构创建以下表格:
🌐 The realtime schema creates the following tables:
schema_migrations- 从 Realtime 跟踪已在数据库上运行的迁移subscription- 跟踪 Postgres 变更的订阅者messages- 每天分区的表,用于数据库中的授权和广播-
授权:通过检查给定用户是否可以读写此表来查看加入的授权策略
-
来自数据库的广播:复制槽跟踪对该表的发布,以将更改广播给已连接的客户端。
-
表格中的模式如下:
1create table realtime.messages (2topic text not null, -- The topic of the message3extension text not null, -- The extension of the message (presence, broadcast)4payload jsonb null, -- The payload of the message5event text null, -- The event of the message6private boolean null default false, -- If the message is going to use a private channel7updated_at timestamp without time zone not null default now(), -- The timestamp of the message8inserted_at timestamp without time zone not null default now(), -- The timestamp of the message9id uuid not null default gen_random_uuid (), -- The id of the message10constraint messages_pkey primary key (id, inserted_at)) partition by RANGE (inserted_at);
-
实时有一个清理过程,会删除超过三天的表。
🌐 Realtime has a cleanup process that will delete tables older than 3 days.
函数 #
🌐 Functions
Realtime 会在你的数据库上创建一些功能:
🌐 Realtime creates some functions on your database:
realtime.send- 在realtime.messages表中插入一条记录,这会触发复制槽将更改广播给客户端。它还会捕获错误,以防触发器出问题。realtime.send_binary- 类似于realtime.send,但允许你广播二进制文件。realtime.broadcast_changes- 使用realtime.send以与 Postgres 更改兼容的格式广播更改