跳到主要内容

subscribe()

subscribe(callback?: (status: REALTIME_SUBSCRIBE_STATES, err?: Error) => void, timeout?: number): RealtimeChannel

向服务端注册频道。未连接时会自动 connect()

status含义
SUBSCRIBED订阅成功
CHANNEL_ERROR订阅失败,查看 err(含 cause
TIMED_OUT服务端超时未响应
CLOSED频道被关闭

参数

callback
(status, err?) => void

订阅状态回调

timeout
number

超时时间(ms)

返回

RealtimeChannel
RealtimeChannel

当前频道,便于链式调用

示例

channel.subscribe((status, err) => {
if (status === "CHANNEL_ERROR" || status === "TIMED_OUT") {
console.error(status, err);
}
});