Javascript SDK Implementation
Join Room

Initializing the Samvyo SDK

After completing the installation steps for the Samvyo SDK, the next step is to initialize the SDK in your application. This involves two main steps:

  1. Initializing the SDK with basic parameters
  2. Joining a room with additional configuration options

Step-by-Step Guide to Initialize and Join a Room

  1. Initialize the SDK: First, initialize the SDK with the required session token and room ID.

  2. Listen for Initialization Success: Set up a listener for the initSuccess event to know when initialization is complete.

  3. Join the Room: Once initialization is successful, you can call the joinRoom method with your desired configuration.

  4. Handle Connection Success or Errors: Implement error handling to manage any potential issues during the process.

Sample Code

Here's a sample code snippet demonstrating the new initialization and joining flow:

import samvyo from "samvyo-js-sdk";
 
async function initializeAndJoinRoom() {
  const sessionToken = "your-session-token"; // Obtain this from your backend server
  const roomId = "your-room-id"; // Specify the room ID you want to join
  const peerName = "your-peer-name"; // Optionally specify a peer name
 
  try {
    // Step 1: Initialize the SDK
    const samvyoInstance = await samvyo.JsSdk.init({
      sessionToken,
      roomId,
      peerName
    });
 
    // Step 2: Listen for initialization success
    samvyoInstance.on('initSuccess', () => {
      console.log("SDK initialized successfully");
      
      // Step 3: Join the room with additional parameters
      samvyoInstance.joinRoom({
        produce: true, // Set to true to send audio/video streams
        consume: true, // Set to true to receive audio/video streams
        // Additional parameters can be added here
      });
    });
 
    // Handle initialization errors
    samvyoInstance.on('initError', (error) => {
      console.error("Error initializing SDK:", error);
    });
 
  } catch (error) {
    console.error("Error in initialization process:", error);
  }
}

Initialization Parameters

  • sessionToken: The token obtained from your backend server, required for authentication.
  • roomId: The ID of the room you want to join.
  • peerName: (Optional) A name for the peer that represents the user in the room.

Join Room Parameters

Once initialization is successful, you can join the room with the following parameters:

  • produce: Set to true if you want to send your audio and video streams to the room.
  • consume: Set to true if you want to receive audio and video streams from other participants in the room.
  • forcePCMU: This parameter forces the use of the PCMU audio codec. The default audio codec used is OPUS. PCMU is particularly useful in environments with varying network conditions, as it ensures stable audio quality and low latency. The default value of this setting is false.
  • forceH264: This parameter forces the use of H264 video codec for efficient compression and high-quality video streaming. The default video codec is VP8. This codec works well across different devices and network conditions. The default value of this setting is false.
  • h264Profile: This parameter specifies the H264 profile for video encoding, which decides the video compression quality. Common profiles are low and high. The default value of this setting is high.
  • forceFPS: This parameter sets the video frame rate, which directly affects the smoothness of the video. A higher frame rate requires more processing power but provides smoother motion, while low frame rate uses less resources results in a less smoother video. The default value of this setting is 25.
  • enableWebcamLayers: This parameter enables simulcast layers for the webcam, which allows different resolutions to be sent simultaneously. This feature ensures that participants with varying network conditions can receive the most appropriate video quality. The default value of this setting is true.
  • numSimulcastStreams: This parameter specifies the number of simulcast streams to produce. The default value of this setting is 3.
  • videoBitRates: This parameter controls the bit rates for different simulcast layers, affecting the video quality and bandwidth usage. You can adjust the bit rates to change the video quality for different layers. By default this setting will take 500, 200, 75 for high, medium and low bitrates respectively.
  • autoGainControl: This parameter enables automatic gain control for the microphone. This helps maintain consistent audio level by adjusting the microphone sensitivity in response to changes in speaking volume. This will be very useful when you move closer or further from the microphone. The default value of this setting is true.
  • echoCancellation: This parameter when enabled prevents the feedback loop by detecting and eliminating the echo from the audio signal. It ensures that when someone speaks, their voice is not picked up by their own microphone and sent back to them or others in the call. By default this setting is true.
  • noiseSuppression: This parameter when enabled reduces background noise, ensuring that your voice remains clear even in noisy environments. By default this setting is true.
  • sampleRate: This parameter specifies the audio sample rate, which determines the quality of the audio stream. Higher sample rates(1600Hz) result in better audio but requires more processing power, while lower sample rates are efficient but result in lower audio quality. For PCMU, this setting should be set to 8000. Otherwise, default setting for this parameter is 44000.
  • channelCount: This parameter refers to the number of audio channels that are captured and transmitted in an audio stream. It determines whether the audio is mono (a single audio channel with the same signal sent to all speakers) or stereo (two audio channels, with one for the left speaker and one for the right). The OPUS codec supports both mono and stereo signals. Depending on the device's capabilities and CPU performance, the channel count can be increased to 2. The default setting for this parameter is 1 (mono).

Summary

The current flow separates the initialization and joining processes, providing developers with greater control over the setup. After successful initialization, you can configure your UI to display a join button. Once the user clicks it, you can invoke the joinRoom method with your desired configuration. Ensure to implement appropriate error handling and UI updates based on the initialization and joining events.

For information about handling events after joining the room, refer to the Event Emitters documentation. To check other methods, refer to Methods.