跳轉到主要內容

安裝

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 中。