Skip to content
Realtime

实时概念

概念 #

🌐 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.

数据库资源 #

🌐 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 拉取
Nano12222
Micro12222
Small15444
Medium15444
Large15444
XL110777
2XL110777
4XL110777
8XL115999
12XL115999
16XL115999
>16XL115999

复制槽 #

🌐 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 - 每天分区的表,用于数据库中的授权和广播
    • 授权:通过检查给定用户是否可以读写此表来查看加入的授权策略

    • 来自数据库的广播:复制槽跟踪对该表的发布,以将更改广播给已连接的客户端。

    • 表格中的模式如下:

      1
      create table realtime.messages (
      2
      topic text not null, -- The topic of the message
      3
      extension text not null, -- The extension of the message (presence, broadcast)
      4
      payload jsonb null, -- The payload of the message
      5
      event text null, -- The event of the message
      6
      private boolean null default false, -- If the message is going to use a private channel
      7
      updated_at timestamp without time zone not null default now(), -- The timestamp of the message
      8
      inserted_at timestamp without time zone not null default now(), -- The timestamp of the message
      9
      id uuid not null default gen_random_uuid (), -- The id of the message
      10
      constraint messages_pkey primary key (id, inserted_at)) partition by RANGE (inserted_at);

函数 #

🌐 Functions

Realtime 会在你的数据库上创建一些功能:

🌐 Realtime creates some functions on your database:

  • realtime.send - 在 realtime.messages 表中插入一条记录,这会触发复制槽将更改广播给客户端。它还会捕获错误,以防触发器出问题。
  • realtime.send_binary - 类似于 realtime.send,但允许你广播二进制文件。
  • realtime.broadcast_changes - 使用 realtime.send 以与 Postgres 更改兼容的格式广播更改