跳转到主要内容

安装

Add InsForge to your Swift Package Manager dependencies:

Enable Logging (Optional)

For debugging, you can configure the SDK log level and destination:
Log Levels:
LevelDescription
.traceMost verbose, includes all internal details
.debugDetailed information for debugging
.infoGeneral operational information (default)
.warningWarnings that don’t prevent operation
.errorErrors that affect functionality
.criticalCritical failures
Log Destinations:
DestinationDescription
.consoleStandard output (print)
.osLogApple’s unified logging system (recommended for iOS/macOS)
.noneDisable logging
.customProvide your own LogHandler factory
Use .info or .error in production to avoid exposing sensitive data in logs.

思维模型

Swift SDK 通过 Socket.IO 连接到 InsForge 实时。订阅频道名称,如 order:123chat:room-1,然后监听发布到这些频道的事件名称。 事件由数据库触发器发布,这些触发器调用 realtime.publish(...) 或由已加入频道的客户端发布。有关频道和 RLS 模型的详细信息,请参阅实时概览

快速入门

connect()

建立实时连接。

subscribe()

订阅一个频道并接收发布到该频道的消息。
如果在 realtime.channels 上启用了 RLS,订阅会根据 SELECT 策略进行检查。

publish()

发布一个事件到频道。
客户端必须订阅频道才能发布到该频道。
如果在 realtime.messages 上启用了 RLS,发布也会根据 INSERT 策略进行检查。

unsubscribe()

离开频道。

disconnect()

关闭实时连接。

频道 API

频道 API 为一个频道名称分组操作。

广播消息

监听事件:
通过频道发布:
不再需要频道对象时将其移除:

状态

成功订阅会返回当前状态快照,SDK API 支持时。通过频道或 SDK 公开的较低级事件 API 监听状态更新。 状态是临时的在线状态。它不存储在 Postgres 中。