Browser SDK
@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.
Migrating from AntMedia
Section titled “Migrating from AntMedia”Swap the import (or the <script> tag) and you’re most of the way there:
// beforeimport { WebRTCAdaptor } from "@antmedia/webrtc_adaptor";// afterimport { 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.
Install / include
Section titled “Install / include”npm
npm install @streamhub/adaptor livekit-clientlivekit-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.
How it gets a LiveKit token
Section titled “How it gets a LiveKit token”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:
- Pre-minted — pass
token+wsUrldirectly in the constructor. No network call from the browser at all; recommended for production — mint the token server-side with yoursk_API token so it never touches the client. - Mint client-side — pass
streamhubTokenUrl(orstreamhubApiUrl+appName) and optionallystreamhubApiToken; the adaptor POSTs the token request itself. - Derive from
websocket_url— an AntMedia-stylewss://host/<app>/websocketURL is parsed intohttps://host/api/v1/apps/<app>/tokens, so an existing AntMedia integration that only configureswebsocket_urlkeeps working with zero other changes.
Constructor options (subset)
Section titled “Constructor options (subset)”| 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 |
Publish / play example
Section titled “Publish / play example”<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.htmlsample pages every app gets under/apps/{app}/samples. - For anonymous, no-login playback (public
/play//embedpages, 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 thechat/reactiontopics directly through the underlyinglivekit-clientinstance — see App endpoints → Streams and Webhooks → chat/reaction events.