跳到正文

实时

实时令牌、频道、发布与在线状态。

1. 概览

基础路径: https://api.infrai.cc/v1/realtime
鉴权头: Authorization: Bearer $INFRAI_API_KEY
bash
# Call any /v1/realtime capability over raw HTTP — no SDK to install.
# curl:
curl https://api.infrai.cc/v1/realtime/... \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json"

2. 方法 (11)

2.1realtime.publish

POST /v1/realtime/publish

向频道发布事件。

参数

名称类型必填说明
channelstring
必填
频道名称。
eventstring
必填
事件名称。
default: "message"
dataunknown
必填
要发布的事件负载。
idempotency_keystring可选可选去重 key;相同重试将返回同一结果。

返回

{ message_id }
名称类型说明
event_idstringevent的唯一标识符
channelstring频道名称或标识符
published_atstring事件发布时间(ISO 8601)
format: date-time
vendorstring处理此请求的供应商

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/realtime/publish \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"channel": "sample"}'

2.2realtime.token.issue

POST /v1/realtime/token/issue

签发限定频道与能力的客户端令牌。

参数

名称类型必填说明
user_idstring可选连接用户的 id。
channelsstring[]可选令牌可访问的频道。
ttl_secondsnumber可选过期前的有效时长(秒)。
≥ 1

返回

RealtimeToken { token, client_id, expires_at, endpoint }
名称类型说明
tokenstring认证或验证令牌
jtistringJWT ID;用于服务端吊销。
channelsstring[]channel names the token grants access to列表
capabilities("subscribe" | "publish" | "presence" | "history")[]capability permissions (publish, subscribe)列表
expires_atstring资源或令牌过期时间(ISO 8601)
format: date-time

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/realtime/token/issue \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"client_id": "sample"}'

2.3realtime.channel.create

POST /v1/realtime/channel/create

创建实时频道。

参数

名称类型必填说明
namestring
必填
频道名称。
type"public" | "private" | "presence"可选频道可见性类型。
default: "public"

返回

{ channel_id, name, type }
名称类型说明
channel_idstringchannel的唯一标识符
namestring资源的可读名称
type"public" | "private" | "presence"资源类型标识
vendorstring处理此请求的供应商
created_atstring资源创建时间(ISO 8601)
format: date-time
member_countinteger | null频道成员数
≥ 0
last_published_atstring | nullISO 8601 时间戳:the last published event(ISO 8601)
format: date-time

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/realtime/channel/create \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"channel": "sample"}'

2.4realtime.presence.get

GET /v1/realtime/presence/get/{channel}

获取频道当前的在线成员。

参数

名称类型必填说明
channelstring
必填
频道名称。

返回

{ members: Array<{ client_id, user_id? }> }
名称类型说明
channelstring频道名称或标识符
membersobject[]presence members in the channel列表
members[].client_idstring已连接用户的客户端标识符
members[].joined_atstring此成员加入的 ISO 8601 时间戳
format: date-time
members[].user_dataobject | null与在线成员关联的自定义数据
next_cursorstring | null传入 presence.get() 以获取下一页;最后一页为 null。

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/realtime/presence/get/CHANNEL \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.5realtime.channel.get

GET /v1/realtime/channel/get/{channel}

获取单个实时频道的详情,包括类型、厂商、在线人数等。

参数

名称类型必填说明
channelstring
必填
要查询的频道名。

返回

Channel { channel_id, name, type, vendor, created_at, member_count?, last_published_at? }
名称类型说明
channel_idstringchannel的唯一标识符
namestring资源的可读名称
type"public" | "private" | "presence"资源类型标识
vendorstring处理此请求的供应商
created_atstring资源创建时间(ISO 8601)
format: date-time
member_countinteger | null频道成员数
≥ 0
last_published_atstring | nullISO 8601 时间戳:the last published event(ISO 8601)
format: date-time

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/realtime/channel/get/CHANNEL \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.6realtime.channel.delete

DELETE /v1/realtime/channel/delete/{channel}

删除一个实时频道并断开其订阅者。

参数

名称类型必填说明
channelstring
必填
要删除的频道名。
idempotency_keystring可选幂等键,用于安全重试。

返回

ChannelDeleteResult { channel, deleted, status }
名称类型说明
channelstring频道名称或标识符
deletedboolean资源是否已成功删除
status"deleted" | "not_found"当前资源状态

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X DELETE https://api.infrai.cc/v1/realtime/channel/delete/CHANNEL \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.7realtime.channel.list

GET /v1/realtime/channel/list

分页列出当前账户下的实时频道。

参数

名称类型必填说明
cursorstring可选翻页游标,传入上一页返回的 next_cursor。
limitnumber可选单页返回的最大条数。

返回

ChannelListResult { channels: Channel[], next_cursor? }
名称类型说明
channelsobject[]channel names the token grants access to列表
channels[].channel_idstringchannel的唯一标识符
channels[].namestring此资源的可读名称
channels[].type"public" | "private" | "presence"资源类型标识
channels[].vendorstring处理此请求的供应商
channels[].created_atstring此资源创建的 ISO 8601 时间戳
format: date-time
channels[].member_countinteger | null频道成员数
≥ 0
channels[].last_published_atstring | null最近发布事件的 ISO 8601 时间戳
format: date-time
next_cursorstring | null传入 channel.list() 以获取下一页;最后一页为 null。

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/realtime/channel/list \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.8realtime.event.types

GET /v1/realtime/event/types

列出可订阅的实时事件类型封闭集合。

返回

EventTypesResult { types: string[] }
名称类型说明
types("channel.opened" | "channel.closed" | "message.published" | "presence.join" | "presence.leave")[]available event types列表

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/realtime/event/types \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.9realtime.publish.batch

POST /v1/realtime/publish/batch

在一次调用中向多个频道批量发布消息。

参数

名称类型必填说明
messagesArray<{ channel: string; event: string; data: unknown }>
必填
要发布的消息列表,每条含 channel、event 和 data。
≥ 1 item
idempotency_keystring可选幂等键,用于安全重试,避免重复发布。

返回

PublishBatchResult { published }
名称类型说明
publishedintegermessages accepted for delivery.数量
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/realtime/publish/batch \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"channel": "sample", "event": "sample", "data": "sample"}]}'

2.10realtime.token.revoke

POST /v1/realtime/token/revoke

吊销先前签发的订阅令牌(登出或封禁时使用)。

参数

名称类型必填说明
token_or_jtistring
必填
完整令牌或其 jti 标识。
≥ 1 chars
idempotency_keystring可选幂等键,用于安全重试。

返回

TokenRevokeResult { revoked }
名称类型说明
revokedboolean令牌是否已成功吊销

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/realtime/token/revoke \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"token_or_jti": "sample"}'

2.11realtime.user.disconnect

POST /v1/realtime/user/disconnect

强制将某客户端踢下线,可选限定到单个频道。

参数

名称类型必填说明
client_idstring
必填
要断开连接的客户端身份标识。
≥ 1 chars
channelstring可选将断开限定到此频道;省略则全部断开。
idempotency_keystring可选幂等键,用于安全重试。

返回

UserDisconnectResult { disconnected }
名称类型说明
disconnectedboolean用户是否已成功断开

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/realtime/user/disconnect \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"client_id": "sample"}'
高级:指定 vendor

默认情况下 infrai 会把每次调用智能路由到最佳可用供应商——无需自己挑选 vendor。作为高级逃生口,本能力支持可选的 vendor 入参以锁定某个供应商。本能力当前所有可用 vendor 可通过该能力 id 对应的 discovery 端点实时获取——参见 discovery API

GET /v1/discovery/{capability}

realtime.channel.create

3. 全部能力

本模块全部已路由能力——完整的对外 REST 契约。上方方法是带讲解的入门示例,此表是完整参考。

realtime.channel.createPOST /v1/realtime/channel/create

Create a realtime pub/sub channel; idempotent.

参数 (4)
名称类型必填说明
channelstring必填Channel name to create.
typestring可选Channel type (e.g. public, private, presence).
default: "public"
vendorstring | null可选Pin to a specific fan-out vendor; falls back to the registry default.
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
realtime.channel.deleteDELETE /v1/realtime/channel/delete/{channel}

Delete a realtime channel and disconnect its subscribers.

参数 (1)
名称类型必填说明
channelstring必填Path parameter.
realtime.channel.getGET /v1/realtime/channel/get/{channel}

Get a single realtime channel's details (type, vendor, online count, etc.).

参数 (1)
名称类型必填说明
channelstring必填Path parameter.
realtime.channel.listGET /v1/realtime/channel/list

List the account's realtime channels with pagination.

无请求参数。

realtime.event.typesGET /v1/realtime/event/types

List the closed set of subscribable realtime event types.

无请求参数。

realtime.presence.getGET /v1/realtime/presence/get/{channel}

Get the current presence state of members online in a channel.

参数 (1)
名称类型必填说明
channelstring必填Path parameter.
realtime.publishPOST /v1/realtime/publish

Publish an event message to a realtime pub/sub channel.

参数 (5)
名称类型必填说明
channelstring必填Channel to publish to.
eventstring可选Event name.
default: "message"
dataany可选Event payload (any JSON value).
account_idstring | null可选Account identifier that owns this resource
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
realtime.publish.batchPOST /v1/realtime/publish/batch

Publish messages to multiple channels in a single batch call.

参数 (2)
名称类型必填说明
messagesobject[]必填List of messages to publish or process
≥ 1 item
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
realtime.token.issuePOST /v1/realtime/token/issue

Issue a short-lived infrai-HMAC client auth token for pub/sub realtime channels (NOT for AI voice sessions — see ai.voice.session).

参数 (5)
名称类型必填说明
client_idstring必填Identity the issued token is bound to.
≥ 1 chars
channelsstring[] | null可选Channels the token may attach to; omit for an account-scoped token.
capabilities("subscribe" | "publish" | "presence" | "history")[] | null可选Capability bits; constrained to the RealtimeCapability closed set. Defaults to subscribe.
ttl_secondsinteger | null可选Requested lifetime; clamped to MAX_TTL_SECONDS (<=0 or invalid -> REALTIME_TOKEN_TTL_INVALID 400).
≥ 1
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
realtime.token.revokePOST /v1/realtime/token/revoke

Revoke a previously issued subscription token (e.g., on logout or ban).

参数 (2)
名称类型必填说明
token_or_jtistring必填Either the full token or its jti.
≥ 1 chars
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
realtime.user.disconnectPOST /v1/realtime/user/disconnect

Forcibly disconnect a client, optionally scoped to a single channel.

参数 (3)
名称类型必填说明
client_idstring必填Client identifier for the connected user
≥ 1 chars
channelstring | null可选Restrict the disconnect to this channel; omit for all.
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry

4. 完整示例

本模块的生产级端到端范例:先一次性配置,再运行业务流程,尽量覆盖本模块的多数 API。

单文件可运行 Python 程序(仅标准库、无 SDK):拷贝后填入 INFRAI_API_KEY 运行,即可按真实业务流逐步体验本模块核心 API——每一步都真实调用并计费,后续步骤复用前一步返回的真实字段。12 行 helper 就是全部集成代码。

python
#!/usr/bin/env python3
"""Infrai · realtime — runnable real-app example (single file, zero deps).

Copy this file, set your key, run it: every step is a REAL call to
api.infrai.cc, billed at the real (tiny) per-call price, printing the
live JSON response. Get a key at https://infrai.cc/login (Google/
GitHub sign-in grants $2 free credit); add funds at
https://infrai.cc/billing. No SDK — the 12-line helper below is the
entire integration."""
import json
import os
from urllib import error, request

KEY = os.environ.get("INFRAI_API_KEY") or "ifr_..."  # <- your key
BASE = "https://api.infrai.cc"


# Same raw HTTPS POST/GET as every per-method example on this page —
# wrapped once for reuse. There is nothing else to it: no SDK.
def infrai(method, path, body=None):
    req = request.Request(
        BASE + path, method=method,
        data=json.dumps(body).encode() if body is not None else None,
        headers={"Authorization": f"Bearer {KEY}",
                 "Content-Type": "application/json"})
    try:
        with request.urlopen(req, timeout=60) as r:
            return json.loads(r.read())
    except error.HTTPError as e:
        return json.loads(e.read())


def show(label, resp):
    print(f"\n== {label} ==")
    print(json.dumps(resp, indent=2, ensure_ascii=False))
    return resp


# 1) realtime.publish — POST /v1/realtime/publish · Publish an event message to a realtime pub/sub channel.
r1 = show("realtime.publish", infrai("POST", "/v1/realtime/publish", {"channel":"room-42","event":"message","data":{"text":"hi"}}))

# 2) realtime.token.issue — POST /v1/realtime/token/issue · Issue a short-lived infrai-HMAC client auth token for pub/sub realtime channels (NOT for AI voice sessions — see ai.voice.session).
r2 = show("realtime.token.issue", infrai("POST", "/v1/realtime/token/issue", {"client_id":"user_42","channels":["room-42"],"capabilities":["subscribe","presence"],"ttl_seconds":3600}))

# 3) realtime.channel.create — POST /v1/realtime/channel/create · Create a realtime pub/sub channel; idempotent.
r3 = show("realtime.channel.create", infrai("POST", "/v1/realtime/channel/create", {"name":"room-42","type":"presence"}))

# 4) realtime.channel.list — GET /v1/realtime/channel/list · List the account's realtime channels with pagination.
r4 = show("realtime.channel.list", infrai("GET", "/v1/realtime/channel/list"))