JavaScript: Realtime
Listen on channels over WebSocket with automatic reconnects, publish messages, see who is online and catch up on missed messages with @sovaria/sdk.
sovaria.realtime.channel() opens a WebSocket on a channel and keeps it open: it reconnects with backoff, gets a fresh ticket each time, and tells you its status. Channel rules decide who may subscribe and publish.
import { createClient } from '@sovaria/sdk'
const sovaria = createClient('https://<project-ref>.sovaria.in', '<anon-key>')
const room = sovaria.realtime.channel('room:42', {
onMessage: (msg) => render(msg),
onStatus: (status) => setBadge(status), // 'connecting' | 'live' | 'reconnecting' | 'closed'
onError: (err) => console.warn(err),
})
await room.send({ text: 'Hello!' })
// later
room.close()sovaria.realtime
sovaria.realtime.subscribe
Subscribe.
sovaria.realtime.subscribe(channel: string, clientId: string): Promise<JsonObject>Parameters
channelstringrequiredclientIdstringrequired
- Returns
Promise<JsonObject>- HTTP
POST /api/v1/rt/stream/subscribe
sovaria.realtime.unsubscribe
Unsubscribe.
sovaria.realtime.unsubscribe(channel: string, clientId: string): Promise<JsonObject>Parameters
channelstringrequiredclientIdstringrequired
- Returns
Promise<JsonObject>- HTTP
POST /api/v1/rt/stream/unsubscribe
sovaria.realtime.presence
Presence.
sovaria.realtime.presence(channel: string): Promise<JsonObject>Parameters
channelstringrequired
- Returns
Promise<JsonObject>— Who is connected to the channel right now.- HTTP
GET /api/v1/rt/stream/presence/{…}
sovaria.realtime.channels
Channels.
sovaria.realtime.channels(): Promise<JsonObject>- Returns
Promise<JsonObject>- HTTP
GET /api/v1/rt/stream/channels
sovaria.realtime.replay
Replay.
sovaria.realtime.replay(
channel: string,
after?: number = 0,
limit?: number = 200,
): Promise<JsonObject>Parameters
channelstringrequiredafternumberoptionaldefault0- Sequence number of the last message you saw.
limitnumberoptionaldefault200- Maximum messages to return.
- Returns
Promise<JsonObject>— Messages afterafter, oldest first — use it to catch up after being offline.- HTTP
GET /api/v1/rt/stream/replay/{…}
sovaria.realtime.publish
Publish.
sovaria.realtime.publish(
channel: string,
payload: JsonObject,
clock?: JsonObject,
clockDelta?: JsonObject,
): Promise<JsonObject>Parameters
channelstringrequired- Channel name.
payloadJsonObjectrequired- JSON message.
clockJsonObjectoptionalclockDeltaJsonObjectoptional
- Returns
Promise<JsonObject>- HTTP
POST /api/v1/rt/stream/publish/{…}
await sovaria.realtime.publish('room:42', { text: 'Server says hi' })sovaria.realtime.e2eeBootstrap
E2ee bootstrap.
sovaria.realtime.e2eeBootstrap(
channel: string,
ttlSeconds?: number = 86400,
forceNew?: boolean = false,
): Promise<JsonObject>Parameters
channelstringrequiredttlSecondsnumberoptionaldefault86400forceNewbooleanoptionaldefaultfalse
- Returns
Promise<JsonObject>- HTTP
POST /api/v1/rt/e2ee/keys/bootstrap
sovaria.realtime.e2eeRotate
E2ee rotate.
sovaria.realtime.e2eeRotate(channel: string, ttlSeconds?: number = 86400): Promise<JsonObject>Parameters
channelstringrequiredttlSecondsnumberoptionaldefault86400
- Returns
Promise<JsonObject>- HTTP
POST /api/v1/rt/e2ee/keys/rotate
sovaria.realtime.e2eeRevoke
E2ee revoke.
sovaria.realtime.e2eeRevoke(
channel: string,
keyId?: string,
reason?: string,
): Promise<JsonObject>Parameters
channelstringrequiredkeyIdstringoptionalreasonstringoptional
- Returns
Promise<JsonObject>- HTTP
POST /api/v1/rt/e2ee/keys/revoke
sovaria.realtime.e2eeActiveKey
Pass your RSA-OAEP (SHA-256) public key — PEM or base64 SPKI — to get the key wrapped for you.
sovaria.realtime.e2eeActiveKey(channel: string, clientPublicKey?: string): Promise<JsonObject>Parameters
channelstringrequiredclientPublicKeystringoptional
- Returns
Promise<JsonObject>- HTTP
GET /api/v1/rt/e2ee/keys/active/{…}
- Part of end-to-end encrypted channels; see Realtime.
sovaria.realtime.e2eeGetKey
E2ee get key.
sovaria.realtime.e2eeGetKey(
channel: string,
keyId: string,
clientPublicKey?: string,
): Promise<JsonObject>Parameters
channelstringrequiredkeyIdstringrequiredclientPublicKeystringoptional
- Returns
Promise<JsonObject>- HTTP
GET /api/v1/rt/e2ee/keys/{…}/{…}
sovaria.realtime.e2eeListKeys
E2ee list keys.
sovaria.realtime.e2eeListKeys(channel: string): Promise<JsonObject>Parameters
channelstringrequired
- Returns
Promise<JsonObject>- HTTP
GET /api/v1/rt/e2ee/keys/{…}
sovaria.realtime.wsTicket
Ws ticket.
sovaria.realtime.wsTicket(channel: string): Promise<string>Parameters
channelstringrequired
- Returns
Promise<string>
sovaria.realtime.wsUrl
Ws url.
sovaria.realtime.wsUrl(channel: string, ticket?: string): stringParameters
channelstringrequiredticketstringoptional
- Returns
string
sovaria.realtime.wsUrlWithTicket
Ws url with ticket.
sovaria.realtime.wsUrlWithTicket(channel: string): Promise<string>Parameters
channelstringrequired
- Returns
Promise<string>
sovaria.realtime.channel
Listen on a channel over WebSocket.
sovaria.realtime.channel(name: string, handlers?: RealtimeChannelHandlers = {}): RealtimeChannelReconnects with backoff (a fresh ticket each time) and hands you each message's payload. Call close() when done.
``ts const ch = sov.realtime.channel('book:42', { onMessage: (m) => console.log(m), onStatus: setStatus }) await ch.send({ type: 'typing' }) ch.close()
Parameters
namestringrequired- Channel name, e.g.
room:42. handlersRealtimeChannelHandlersoptionaldefault{}onMessage(payload, raw),onStatus(status),onError(error),maxBackoffMs(default 15000).
- Returns
RealtimeChannel— A channel withname,status,send(payload)andclose().