0.3.1 • Published 2 months ago

@jellyfish-dev/ts-client-sdk v0.3.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 months ago

Jellyfish TS client

TypeScript client library for Jellyfish.

Documentation

Documentation is available here

Installation

You can install this package using npm:

npm install @jellyfish-dev/ts-client-sdk

It was tested with node.js version specified in .tool-versions file.

Usage

Prerequisites:

  • Running Jellyfish server.
  • Created room and token of peer in that room. You u can use dashboard example to create room and peer token.

This snippet is based on minimal example.

import { JellyfishClient } from "@jellyfish-dev/ts-client-sdk";
import { WebRTCEndpoint } from "@jellyfish-dev/membrane-webrtc-js";

const SCREEN_SHARING_MEDIA_CONSTRAINTS = {
  video: {
    frameRate: { ideal: 20, max: 25 },
    width: { max: 1920, ideal: 1920 },
    height: { max: 1080, ideal: 1080 },
  },
};

// Example metadata types for peer and track
// You can define your own metadata types just make sure they are serializable
type PeerMetadata = {
  name: string;
};

type TrackMetadata = {
  type: "camera" | "screen";
};

// Creates a new JellyfishClient object to interact with Jellyfish
const client = new JellyfishClient<PeerMetadata, TrackMetadata>();

const peerToken = prompt("Enter peer token") ?? "YOUR_PEER_TOKEN";

// Start the peer connection
client.connect({
  peerMetadata: { name: "peer" },
  token: peerToken,
  // if websocketUrl is not provided, it will default to ws://localhost:5002/socket/peer/websocket
});

// You can listen to events emitted by the client
client.on("onJoinSuccess", (peerId, peersInRoom) => {
  // Check if webrtc is initialized
  if (!client.webrtc) return console.error("webrtc is not initialized");

  // To start broadcasting your media you will need source of MediaStream like camera, microphone or screen
  // In this example we will use screen sharing
  startScreenSharing(client.webrtc);
});

// To receive media from other peers you need to listen to onTrackReady event
client.on("onTrackReady", (ctx) => {
  const peerId = ctx.peer.id;

  document.getElementById(peerId)?.remove(); // remove previous video element if it exists

  // Create a new video element to display the media
  const videoPlayer = document.createElement("video");
  videoPlayer.id = peerId;
  videoPlayer.oncanplaythrough = function () {
    // Chrome blocks autoplay of unmuted video
    videoPlayer.muted = true;
    videoPlayer.play();
  };
  document.body.appendChild(videoPlayer);

  videoPlayer.srcObject = ctx.stream; // assign MediaStream to video element
});

// Cleanup video element when track is removed
client.on("onTrackRemoved", (ctx) => {
  const peerId = ctx.peer.id;
  document.getElementById(peerId)?.remove(); // remove video element
});

async function startScreenSharing(webrtc: WebRTCEndpoint) {
  // Get screen sharing MediaStream
  const screenStream = await navigator.mediaDevices.getDisplayMedia(SCREEN_SHARING_MEDIA_CONSTRAINTS);

  // Add local MediaStream to webrtc
  screenStream.getTracks().forEach((track) => webrtc.addTrack(track, screenStream, { type: "screen" }));
}

Examples

For more examples, see examples folder.

More information about usage of webrtc can be found in MembraneWebRTC documentation.

Contributing

We welcome contributions to Jellyfish Ts Client SDK. Please report any bugs or issues you find or feel free to make a pull request with your own bug fixes and/or features.

Detailed information about contributing can be found in contributing.md.

Jellyfish Ecosystem

Client SDKsReact, React Native, iOs, Android
Server SDKsElixir, Python, OpenAPI
ServicesVideoroom - an example videoconferencing app written in elixir Dashboard - an internal tool used to showcase Jellyfish's capabilities
ResourcesJellyfish Book - theory of the framework, Docs, Tutorials
MembraneJellyfish is based on Membrane, Discord
CompositorCompositor - Membrane plugin to transform video
ProtobufsIf you want to use Jellyfish on your own, you can use our protobufs

Copyright and License

Copyright 2023, Software Mansion

Software Mansion

Licensed under the Apache License, Version 2.0

0.3.1

2 months ago

0.3.0

3 months ago

0.2.5

3 months ago

0.2.4

4 months ago

0.2.3

4 months ago

0.2.1

6 months ago

0.2.0

7 months ago

0.2.2

5 months ago

0.1.3

8 months ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago