CompositionClient
Client for Fishjam compositions. Source: packages/js-server-sdk/src/composition.ts in the js-server-sdk repo (https://github.com/fishjam-cloud/js-server-sdk).
Concepts, scenes, templates, and room composition are covered in
../composition/SKILL.md. This file is the TypeScript surface only.
Constructor
import { CompositionClient } from '@fishjam-cloud/js-server-sdk';
const compositionClient = new CompositionClient({
managementToken: process.env.FISHJAM_MANAGEMENT_TOKEN!,
});- Same package and same management token as
FishjamClient; no Fishjam ID. - Create one per process and share it.
IDs
CompositionId, InputId, OutputId, and RendererId are branded strings. createComposition returns a CompositionId; IDs you choose yourself are cast once:
import type { InputId, OutputId, RendererId } from '@fishjam-cloud/js-server-sdk';
const camera = 'camera' as InputId;
const main = 'main' as OutputId;
const logo = 'logo' as RendererId;Inside scenes, inputId and imageId are plain strings.
Compositions
const { compositionId } = await compositionClient.createComposition({ autostart: false, cleanupWithoutInputs: false });
const url: string = compositionClient.compositionUrl(compositionId);
await compositionClient.startComposition(compositionId);
await compositionClient.deleteComposition(compositionId);compositionUrl is synchronous and makes no request; pass its result to fishjamClient.forwardRoomTracks.
Inputs
const { url, bearerToken } = await compositionClient.registerWhipInput(compositionId, camera);
await compositionClient.registerWhipInput(compositionId, camera, { bearerToken: 'chosen-token', video: false });
const publishUrl: string = await compositionClient.registerRtmpInput(compositionId, encoder, { streamKey });
await compositionClient.registerWhepInput(compositionId, remote, { endpointUrl: whepUrl, bearerToken: viewerToken });
const { videoDurationMs, audioDurationMs } = await compositionClient.registerMp4Input(compositionId, intro, {
url: 'https://smelter.dev/videos/template-scene-race.mp4',
loop: true,
});
await compositionClient.unregisterInput(compositionId, camera);
await compositionClient.unregisterInput(compositionId, camera, { scheduleTimeMs: 10_000 });registerWhipInputreturns the full publish URL, ready to hand to a publisher.registerInput(compositionId, inputId, { type, ... })is the generic form behind these helpers.
Outputs
await compositionClient.registerWhipOutput(compositionId, main, {
endpointUrl: fishjamClient.livestreamWhipUrl(),
bearerToken: streamerToken,
video: { resolution: { width: 1280, height: 720 }, initial: videoScene },
audio: { initial: audioScene },
});
await compositionClient.registerRtmpOutput(compositionId, restream, {
url: ingestUrlWithStreamKey,
video: { resolution: { width: 1920, height: 1080 }, initial: videoScene },
audio: { initial: audioScene },
});
await compositionClient.registerTemplateOutput(
compositionId,
'show' as OutputId,
{
type: 'whip_client',
endpointUrl: fishjamClient.livestreamWhipUrl(),
bearerToken: showStreamerToken,
video: { resolution: { width: 1280, height: 720 }, initial: { root: { type: 'view' } } },
audio: { initial: { inputs: [] } },
},
'./my-template/dist/App.js',
);
await compositionClient.updateOutput(compositionId, main, { video: videoScene, audio: audioScene });
await compositionClient.requestKeyframe(compositionId, main);
await compositionClient.unregisterOutput(compositionId, main);- Each output pushes to its own destination, so
showuses a streamer token of a second livestream room.updateOutputapplies to static outputs likemain, never to a template output. registerOutput(compositionId, outputId, { type, ... })is the generic form behindregisterWhipOutputandregisterRtmpOutput.
Template outputs
registerTemplateOutput(compositionId, outputId, config, template) registers an output and uploads the React template that renders it, in one call. Building the template: ../composition/templates.md.
configis the same objectregisterOutputtakes, withtypeset explicitly ('whip_client'or'rtmp_client').templateis the built bundle (dist/App.js), as a file path or aBlob. A path is read from disk and uploaded, so never pass one taken from user input.video.initialandaudio.initialare placeholders. The template replaces both scenes on its first render; pass{ root: { type: 'view' } }and{ inputs: [] }.- Pass
audioif the output should be heard. Without it the output has no audio track, whatever the template renders. - Change it with
sendEvent, neverupdateOutput: the template owns the scene and its next render replaces your update. - Redeploy a new bundle with
unregisterOutput, thenregisterTemplateOutputagain. Inputs stay registered; resend the events that carry the template's state.
Assets and events
await compositionClient.registerImage(compositionId, logo, { assetType: 'auto', url: 'https://fishjam.swmansion.com/docs/img/logo.svg' });
await compositionClient.unregisterImage(compositionId, logo);
await compositionClient.registerFont(compositionId, './fonts/Inter.ttf');
await compositionClient.sendEvent(compositionId, { eventName: 'SET_CAPTION', data: { text: 'Welcome!' } });registerFont takes a file path or a Blob, like registerTemplateOutput.
Related FishjamClient methods
| Method | Use |
|---|---|
forwardRoomTracks(roomId, compositionUrl) |
Forward a room into a composition (../composition/room-composition.md). |
livestreamWhipUrl() |
endpointUrl for a WHIP output into a Fishjam livestream room. |
createLivestreamStreamerToken(roomId) |
bearerToken for that output (livestream-and-moq.md). |
createLivestreamViewerToken(roomId) |
bearerToken for a WHEP input pulling a private Fishjam livestream. |
Types
Scene and config types are exported as types only: VideoScene, AudioScene, Component, View, Tiles, Rescaler, InputStream, Text, Image, Transition, RegisterOutput, WhipOutput, RtmpOutput, ImageSpec, CreateCompositionRequest, UpdateOutputRequest, and more. Write enum-like fields as string literals (type: 'rescaler', mode: 'fill').
import type { AudioScene, VideoScene } from '@fishjam-cloud/js-server-sdk';Exception types
Composition calls throw the same FishjamBaseException hierarchy as FishjamClient (client.md), plus not-found types for composition resources:
| Exception | When |
|---|---|
CompositionNotFoundException |
The composition does not exist, for example after idle cleanup or deletion. |
InputNotFoundException, OutputNotFoundException, RendererNotFoundException |
The input, output, or image does not exist. |
BadRequestException |
Invalid body, such as a mismatched updateOutput or an odd resolution. details carries the server message. |
UnauthorizedException |
Wrong management token. |
ServiceUnavailableException |
Temporarily unavailable; retry with backoff. |