1.1.3 • Published 2 years ago

react-native-huddle-client v1.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

HuddleClient React Native SDK

Getting Started

  1. Get the Huddle01 React Native SDK
$ npm install --save react-native-huddle-client

OR

$ yarn add react-native-huddle-client
  1. Get your API Key: You can get your access keys in the Developer section of the Huddle01 Dashboard
  2. Import modules & instantiate Huddle Client.
import HuddleClient, { emitter } from "react-native-huddle-client";

The infrastructure mandates a schema on the URL of the type https://domain.com/room?roomId=C132

Initialise a new object

const huddle = new HuddleClient(config);

An example config object can be given as

const config: HuddleClientConfig = {
  apiKey: "<API Key>",          // API KEY (issued on the dashboard)
  hostname: "domain.com:4443",  // Domain name with 4443 port
  roomId: "C132",               // Room ID
  peerId: "rick254",            // Peer ID (needs to be unique for every peer)
  displayName: "Rick Sanchez",  // Display Name
  isBot,                        // bot flag
};

Setting up event listeners

The emitter that we imported in the 1st step is used to emit events by Huddle about your application. Please refer to the demo app for application references

The various types of events are:

  1. Trigger: on room status changes
    Return value: error object

     emitter.on("roomState", (state) => {
       //do whatever (ideally switch-case between all state types)
     });

    Different state types:

    connectingwaiting_for_admissionconnectedfaileddisconnected
    trying to connect to the roomwaiting admission to join room from host in lobbysuccessfully connected to the roomfailure in connection to the roomsuccessfully disconnected from the room
  2. Trigger: an error event on client/server side
    Return value: error object

     emitter.on("error", (error) => {
       //do whatever
     });
  3. Trigger: new peer joins the room
    Return value: an entire peer object with all the details

     emitter.on("addPeer", (peer) => {
       //do whatever
     });
  4. Trigger: you have a new producer producing to the Huddle servers
    Return value: an producer object with details like your production media track (eg. camera/mic/screenshare)

     emitter.on("addProducer", (producer) => {
       //do whatever (ideally switch-case between all state types)
     });

    Different state types:

    producer.type:

    cameramicscreenshare
    a camera stream consumera mic stream consumera screenshare stream consumer
  5. Trigger: you have a new consumer consuming from the Huddle servers
    Return value: a consumer object with details like your consumption media track (eg. camera/mic/screenshare)

    emitter.on("addConsumer", (consumer: IConsumer) => {
      //do whatever (ideally switch-case between all state types)
    });
#### Different state types:

consumer.type:

cameramicscreenshare
a camera stream consumera mic stream consumera screenshare stream consumer
  1. Trigger: one of the existing peers disconnects from the room
    Return value: an entire peer object with all the details about the peer(same as the object received on the "add" event)

     emitter.on("removePeer", (peer) => {
       //do whatever
     });
  2. Trigger: you have closed the production of your existing producer to the Huddle servers
    Return value: a producer object with details like your production media track (eg. camera/mic/screenshare) peer(same as the object received on the "add" event)

     emitter.on("removeProducer", (producer) => {
       //do whatever (ideally switch-case between all state types)
     });

    Different state types:

    producer.type:

    webcammicscreenshare
    a webcam stream producera mic stream producera screenshare stream producer
  3. Trigger: you have closed the production of your existing producer to the Huddle servers
    Return value: a consumer object with details like your consumption media track (eg. camera/mic/screenshare) peer (same as the object received on the "add" event)

     emitter.on("removeConsumer", (consumer) => {
       //do whatever (ideally switch-case between all state types)
     });

    Different state types:

    consumer.type:

    cameramicscreenshare
    a camera stream consumera mic stream consumera screenshare stream consumer
  4. Trigger: new peer try to join room and waiting admission in lobby
    Return value: an entire peers in lobby(waiting room)

     emitter.on("newLobbyPeer", (peers) => {
       //do whatever
     });
  5. Trigger: one of peer in lobby is admitted, denied or exit lobby
    Return value: an entire peers in lobby(waiting room)

     emitter.on("updatedPeersArray", (peers) => {
       //do whatever
     });

All the data/states received by events need to be maintained by you in your app. Can be achieved using React states/redux or any similar implementations.

Please refer to the demo app where we use local React states to handle these data.

Methods Available:

  • huddle.join()

    Example given below:

    const joinRoom = async () => {
      if (!huddle) return;
      try {
        setupEventListeners();
        await huddle.join();
      } catch (error: any) {
        alert(error);
      }
    };
  • huddle.close()

    close() can only be called after join() is successful

    Example given below:

    const leaveRoom = async () => {
      if (!huddle) return;
      try {
        await huddle.close();
        setRoomState(false);
      } catch (error: any) {
        alert(error);
      }
    };
  • huddle.enableCamera()

  • huddle.enableMic()
  • huddle.unmuteMic()
  • huddle.enableShare()

    Example given below:

    const enableWebcam = async () => {
      if (!huddle) return;
      try {
        await huddle.enableCamera();
        setCameraState(true);
      } catch (error: any) {
        alert(error);
      }
    };
  • huddle.disableCamera()

  • huddle.disableShare()
  • huddle.disableMic()
  • huddle.muteMic()

    Example given below:

    const disableWebcam = async () => {
      if (!huddle) return;
      try {
        await huddle.disableWebcam();
        setCameraState(false);
      } catch (error: any) {
        alert(error);
      }
    };

    NOTE: enable() functions need to be called and have returned success first before calling any of the disable()counterparts

  • huddle.changeCamera()

    Example given below:

    const changeCamera = async () => {
      if (!huddle) return;
      try {
        await huddle.changeCamera();
      } catch (error: any) {
        alert(error);
      }
    };