Skip to content

Browser SDK

This content is for the 1.0 version. Switch to the latest version for up-to-date documentation.

@streamhub/adaptor is the browser SDK for publishing to and playing from StreamHub. It’s built as a drop-in replacement for AntMedia’s @antmedia/webrtc_adaptor WebRTCAdaptor — the same constructor options, methods, and string-based callback events ("initialized", "publish_started", "newStreamAvailable", …) — implemented on top of livekit-client talking to your StreamHub server. WebRTCAdaptor is an alias of StreamHubAdaptor, so existing new WebRTCAdaptor({...}) code keeps working.

Swap the import (or the <script> tag) and you’re most of the way there:

// before
import { WebRTCAdaptor } from "@antmedia/webrtc_adaptor";
// after
import { WebRTCAdaptor } from "@streamhub/adaptor";

Existing switch (info) { case "initialized": ... } callback handlers keep working unmodified, since the adaptor emulates AntMedia’s event names on top of LiveKit’s very different underlying signaling.

**npm**
Terminal window
npm install @streamhub/adaptor livekit-client

livekit-client is a peer dependency for the ESM/CJS builds.

Script tag (CDN-style)

The core server bundles a browser build (with livekit-client inlined) and serves it directly — no separate CDN needed:

<script src="https://your-domain.example.com/sdk/streamhub-adaptor.global.js"></script>

This is the /sdk static mount (outside /api/v1); the file lives under SDK_DIR on the server (default <DATA_DIR>/sdk) and is copied there by the container entrypoint at boot. If it’s ever missing, the mount 404s and the generated sample pages fall back to livekit-client directly.

Under the hood, publishing/playing needs a LiveKit join token minted by POST /apps/{app}/tokens (see App endpoints → Tokens). The adaptor resolves credentials in this priority order:

  1. Pre-minted — pass token + wsUrl directly in the constructor. No network call from the browser at all; recommended for production — mint the token server-side with your sk_ API token so it never touches the client.
  2. Mint client-side — pass streamhubTokenUrl (or streamhubApiUrl + appName) and optionally streamhubApiToken; the adaptor POSTs the token request itself.
  3. Derive from websocket_url — an AntMedia-style wss://host/<app>/websocket URL is parsed into https://host/api/v1/apps/<app>/tokens, so an existing AntMedia integration that only configures websocket_url keeps working with zero other changes.
Option Origin Notes
websocket_url AntMedia wss://host/<app>/websocket; <app> is used to mint tokens
mediaConstraints AntMedia { video, audio } → LiveKit capture options
localVideoElement / localVideoId AntMedia local preview <video> element
bandwidth AntMedia kbps → videoEncoding.maxBitrate
dataChannelEnabled AntMedia default true
isPlayMode / playOnly AntMedia subscribe-only mode (no camera/mic)
callback AntMedia (info, obj) => void — the string-event callback
token + wsUrl StreamHub pre-minted credentials (recommended)
streamhubTokenUrl / streamhubApiUrl + appName / streamhubApiToken StreamHub client-side token minting
<script src="https://your-domain.example.com/sdk/streamhub-adaptor.global.js"></script>
<script>
const adaptor = new WebRTCAdaptor({
streamhubApiUrl: "https://your-domain.example.com/api/v1",
appName: "demo",
streamhubApiToken: "sk_...", // or mint server-side and pass token+wsUrl instead
localVideoId: "local",
mediaConstraints: { video: true, audio: true },
callback: (info, obj) => {
if (info === "initialized") adaptor.publish("room1");
if (info === "publish_started") console.log("live:", obj);
},
});
</script>

For playback-only, set isPlayMode/playOnly and call the adaptor’s play method for the target room/stream — newStreamAvailable fires with the remote track(s) to attach to a <video> element, exactly as it would with the AntMedia SDK.

  • This is the SDK behind the auto-generated webrtc-publish.html / webrtc-play.html sample pages every app gets under /apps/{app}/samples.
  • For anonymous, no-login playback (public /play//embed pages, or a third-party page embedding your stream), mint via the public play-token endpoint instead of an API token — see Authentication → the public play-token.
  • Chat/reaction data-channel messages are available too: any client holding a join token minted with canPublishData: true (the default) can publish/ subscribe to the chat/reaction topics directly through the underlying livekit-client instance — see App endpoints → Streams and Webhooks → chat/reaction events.