Class FishjamClient<PeerMetadata, TrackMetadata>

FishjamClient is the main class to interact with Fishjam.

Example

const client = new FishjamClient<PeerMetadata, TrackMetadata>();
const peerToken = "YOUR_PEER_TOKEN";

// You can listen to events emitted by the client
client.on("joined", (peerId, peersInRoom) => {
console.log("join success");
});

// Start the peer connection
client.connect({
peerMetadata: {},
isSimulcastOn: false,
token: peerToken
});

// Close the peer connection
client.disconnect();

You can register callbacks to handle the events emitted by the Client.

Example


client.on("trackReady", (ctx) => {
console.log("On track ready");
});

Type Parameters

  • PeerMetadata
  • TrackMetadata

Hierarchy

Constructors

Properties

connectConfig: any
handleWebRTCNotInitialized: any
initConnection: any
initWebsocket: any
isOpen: any
leave: (() => void)

Leaves the room. This function should be called when user leaves the room in a clean way e.g. by clicking a dedicated, custom button disconnect. As a result there will be generated one more media event that should be sent to the RTC Engine. Thanks to it each other peer will be notified that peer left in MessageEvents.peerLeft,

Type declaration

    • (): void
    • Returns void

peerMetadataParser: any
reconnectManager: any
removeEventListeners: any
setupCallbacks: any
status: "initialized" | "new"
trackMetadataParser: any
updatePeerMetadata: ((peerMetadata) => void)

Updates the metadata for the current peer.

Type declaration

    • (peerMetadata): void
    • Parameters

      • peerMetadata: PeerMetadata

        Data about this peer that other peers will receive upon joining.

        If the metadata is different from what is already tracked in the room, the event MessageEvents.peerUpdated will be emitted for other peers in the room.

      Returns void

updateTrackMetadata: ((trackId, trackMetadata) => void)

Updates the metadata for a specific track.

Type declaration

    • (trackId, trackMetadata): void
    • Parameters

      • trackId: string

        TrackId (generated in addTrack) of audio or video track.

      • trackMetadata: TrackMetadata

        Data about this track that other peers will receive upon joining.

        If the metadata is different from what is already tracked in the room, the event MessageEvents.trackUpdated will be emitted for other peers in the room.

      Returns void

webrtc: any
websocket: any

Methods

  • Adds track that will be sent to the RTC Engine.

    Parameters

    • track: MediaStreamTrack

      Audio or video track e.g. from your microphone or camera.

    • Optional trackMetadata: TrackMetadata

      Any information about this track that other peers will receive in MessageEvents.peerJoined. E.g. this can source of the track - wheather it's screensharing, webcam or some other media device.

    • Optional simulcastConfig: SimulcastConfig

      Simulcast configuration. By default, simulcast is disabled. For more information refer to SimulcastConfig.

    • Optional maxBandwidth: TrackBandwidthLimit

      Maximal bandwidth this track can use. Defaults to 0 which is unlimited. This option has no effect for simulcast and audio tracks. For simulcast tracks use FishjamClient.setTrackBandwidth.

    Returns Promise<string>

    Returns id of added track

    Example

    const localStream: MediaStream = new MediaStream();
    try {
    const localAudioStream = await navigator.mediaDevices.getUserMedia(
    { audio: true }
    );
    localAudioStream
    .getTracks()
    .forEach((track) => localStream.addTrack(track));
    } catch (error) {
    console.error("Couldn't get microphone permission:", error);
    }

    try {
    const localVideoStream = await navigator.mediaDevices.getUserMedia(
    { video: true }
    );
    localVideoStream
    .getTracks()
    .forEach((track) => localStream.addTrack(track));
    } catch (error) {
    console.error("Couldn't get camera permission:", error);
    }

    localStream
    .getTracks()
    .forEach((track) => client.addTrack(track, localStream));
  • Returns void

  • Uses the WebSocket connection and !WebRTCEndpoint | WebRTCEndpoint to join to the room. Registers the callbacks to handle the events emitted by the !WebRTCEndpoint | WebRTCEndpoint. Make sure that peer metadata is serializable.

    Parameters

    Returns void

    Example

    const client = new FishjamClient<PeerMetadata, TrackMetadata>();

    client.connect({
    peerMetadata: {},
    token: peerToken
    });
  • Disables track encoding so that it will be no longer sent to the server.

    Parameters

    • trackId: string

      Id of track

    • encoding: TrackEncoding

      Encoding that will be disabled

    Returns void

    Example

    const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, active_encodings: ["l", "m", "h"]});
    webrtc.disableTrackEncoding(trackId, "l");
  • Disconnect from the room, and close the websocket connection. Tries to leave the room gracefully, but if it fails, it will close the websocket anyway.

    Returns void

    Example

    const client = new FishjamClient<PeerMetadata, TrackMetadata>();

    client.connect({ ... });

    client.disconnect();
  • Enables track encoding so that it will be sent to the server.

    Parameters

    • trackId: string

      Id of track

    • encoding: TrackEncoding

      Encoding that will be enabled

    Returns void

    Example

    const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, active_encodings: ["l", "m", "h"]});
    webrtc.disableTrackEncoding(trackId, "l");
    // wait some time
    webrtc.enableTrackEncoding(trackId, "l");
  • Returns (string | symbol)[]

  • Returns bigint

  • Returns number

  • Returns boolean

  • Type Parameters

    Parameters

    • event: E

    Returns number

  • Remove a callback from the list of callbacks to be called when the event is emitted.

    Type Parameters

    Parameters

    Returns this

    This

    Example

    const callback = ()=>{  };

    client.on("onJoinSuccess", callback);

    client.off("onJoinSuccess", callback);
  • Register a callback to be called when the event is emitted. Full list of callbacks can be found here MessageEvents.

    Type Parameters

    Parameters

    Returns this

    This

    Example

    const callback = ()=>{  };

    client.on("onJoinSuccess", callback);
  • Type Parameters

    Parameters

    • Optional event: E

    Returns this

  • Removes a track from connection that was being sent to the RTC Engine.

    Parameters

    • trackId: string

      Id of audio or video track to remove.

    Returns Promise<void>

    Example

    // setup camera
    let localStream: MediaStream = new MediaStream();
    try {
    localVideoStream = await navigator.mediaDevices.getUserMedia(
    VIDEO_CONSTRAINTS
    );
    localVideoStream
    .getTracks()
    .forEach((track) => localStream.addTrack(track));
    } catch (error) {
    console.error("Couldn't get camera permission:", error);
    }

    let trackId
    localStream
    .getTracks()
    .forEach((track) => trackId = webrtc.addTrack(track, localStream));

    // remove track
    webrtc.removeTrack(trackId)
  • Replaces a track that is being sent to the RTC Engine.

    Parameters

    • trackId: string

      Id of audio or video track to replace.

    • newTrack: null | MediaStreamTrack

      New audio or video track.

    • Optional newTrackMetadata: TrackMetadata

      Optional track metadata to apply to the new track. If no track metadata is passed, the old track metadata is retained.

    Returns Promise<void>

    Success

    Example

    // setup camera
    let localStream: MediaStream = new MediaStream();
    try {
    localVideoStream = await navigator.mediaDevices.getUserMedia(
    VIDEO_CONSTRAINTS
    );
    localVideoStream
    .getTracks()
    .forEach((track) => localStream.addTrack(track));
    } catch (error) {
    console.error("Couldn't get camera permission:", error);
    }
    let oldTrackId;
    localStream
    .getTracks()
    .forEach((track) => trackId = webrtc.addTrack(track, localStream));

    // change camera
    const oldTrack = localStream.getVideoTracks()[0];
    let videoDeviceId = "abcd-1234";
    navigator.mediaDevices.getUserMedia({
    video: {
    ...(VIDEO_CONSTRAINTS as {}),
    deviceId: {
    exact: videoDeviceId,
    },
    }
    })
    .then((stream) => {
    let videoTrack = stream.getVideoTracks()[0];
    webrtc.replaceTrack(oldTrackId, videoTrack);
    })
    .catch((error) => {
    console.error('Error switching camera', error);
    })
  • Updates maximum bandwidth for the given simulcast encoding of the given track.

    Parameters

    • trackId: string

      Id of the track

    • rid: string

      Rid of the encoding

    • bandwidth: number

      Desired max bandwidth used by the encoding (in kbps)

    Returns Promise<boolean>

  • Parameters

    • maxListeners: number

    Returns this

  • Sets track encoding that server should send to the client library.

    The encoding will be sent whenever it is available. If chosen encoding is temporarily unavailable, some other encoding will be sent until chosen encoding becomes active again.

    Parameters

    • trackId: string

      Id of track

    • encoding: TrackEncoding

      Encoding to receive

    Returns void

    Example

    webrtc.setTargetTrackEncoding(incomingTrackCtx.trackId, "l")
    
  • Updates maximum bandwidth for the track identified by trackId. This value directly translates to quality of the stream and, in case of video, to the amount of RTP packets being sent. In case trackId points at the simulcast track bandwidth is split between all of the variant streams proportionally to their resolution.

    Parameters

    • trackId: string
    • bandwidth: number

      In kbps

    Returns Promise<boolean>

    Success