npm.io
1.1.0 • Published yesterday

rtn-miniprogram-sdk

Licence
MIT
Version
1.1.0
Deps
0
Size
204 kB
Vulns
0
Weekly
0

rtn-miniprogram-sdk

RTN SDK for WeChat Mini Program RTC scenarios.

The package exposes a URL-based Client facade for Mini Program components:

  • publish() returns an RTMP URL for live-pusher.
  • subscribe(uid) returns an RTMP URL for live-player.
  • Signaling uses the shared SignalAccess protocol from @open/infra.
  • Runtime I/O uses Mini Program APIs through wx.request and wx.connectSocket.

Install

import { Client } from 'rtn-miniprogram-sdk';

Basic flow

const client = new Client();

client.setParameters({
  ConfPriCloudAddr: {
    ServerAdd: 'primary.example.com',
    Port: 1443,
    Wss: true,
  },
  ConfPriCloudAddr1: {
    ServerAdd: 'backup.example.com',
    Port: 8080,
    Wss: false,
  },
});

await client.init(appId);
await client.join(token, channel, uid, false);

const pushUrl = await client.publish();
const { url: playUrl } = await client.subscribe(remoteUid);

await client.unpublish();
await client.unsubscribe(remoteUid);
await client.leave();

SignalAccess gateways

Configure custom SignalAccess gateways with the same setParameters contract used by open-rtn-sdk.

ConfPriCloudAddr and ConfPriCloudAddr1 are read when join() creates the SignalAccess session. When no custom gateway is configured, the SDK falls back to the open-rtn mainline default gateway:

https://pro.openrtn.agrtc.cn:1443

Wss controls both the gateway request scheme and the preferred WebSocket transport passed into service.access.resolve:

  • Wss: true uses https gateway requests and expects secure wss signaling nodes.
  • Wss: false uses http gateway requests and expects plain ws signaling nodes.

Lifecycle rules

  • Call init(appId) before join().
  • join() resolves the gateway, connects SignalAccess, sends auth.session.login, stores sessionId, and then reports client.status.update without waiting for a response.
  • leave() sends auth.session.logout with the stored sessionId, closes signaling, and clears local session, publish, and subscribe state.
  • If logout fails, local state is still cleared and the logout error is surfaced to the caller.
  • destroy() closes the local connection and clears state without sending logout.

Publish and subscribe rules

  • publish() is only valid for host.
  • The first publish() sends rtc.stream.publish with allocUrlType: 'rtmp', stores rtmpUrl and pubSessionId, and returns the RTMP push URL.
  • Repeated publish() returns the cached push URL.
  • unpublish() sends rtc.stream.unpublish only when a publish exists. Calling it with no publish is a no-op.
  • subscribe(uid) sends rtc.stream.subscribe with allocUrlType: 'rtmp', stores the returned rtmpUrl, and returns { url, rotation }.
  • Repeated subscribe(uid) returns the cached play URL.
  • unsubscribe(uid) sends rtc.stream.unsubscribe only when a subscription exists. Calling it with no subscription is a no-op.

Role rules

  • setRole(role) before join() only updates local role and writes the selected role into the next login request.
  • setRole(role) after join() sends rtc.stream.setrole.
  • Switching from published host to audience invalidates the existing push URL and clears the local publish session id. The Mini Program demo must clear the bound live-pusher URL at the same time.

Current limitations

  • WHEP playback is intentionally deferred. The current implementation stabilizes RTMP publish and subscribe first.
  • Component status reporting methods, setAudioCodec, and channel media relay APIs are not part of the current public Mini Program SDK surface.
  • muteLocal, unmuteLocal, mute, unmute, setRemoteVideoStreamType, and updatePushUrl return NOT_IMPLEMENTED until matching protocol commands and server behavior are confirmed.
  • SignalAccess reconnect is implemented: initial join retries recoverable SignalAccess failures while staying in CONNECTING, and an established session enters RECONNECTING before logging in again. Native Mini Program live-pusher / live-player publish and subscribe URL recovery remains application-controlled.

Verification

pnpm --filter rtn-miniprogram-sdk run check
pnpm --filter rtn-miniprogram-sdk run build
pnpm --filter rtn-miniprogram-sdk run docs:api
pnpm consume:check:rtn-miniprogram-sdk
pnpm release:check rtn-miniprogram-sdk

Keywords