Javascript SDK Implementation
Session Token

Create Session Token

A session token is a crucial component for establishing secure connections with the Samvyo SDK. It serves as an authentication mechanism that validates your application's access to create and join rooms. The session token is required when initializing a connection with the SDK using the init method.

Why Session Token is Needed?

  1. Authentication: The session token authenticates your application's requests to the Samvyo server
  2. Security: It ensures that only authorized applications can create and join rooms
  3. Session Management: It helps track and manage active sessions for each room
  4. Required for Initialization: The init method requires both roomId and sessionToken to establish a connection

Flow of Session Token Generation

  1. Your application server uses the access key and secret access key (obtained from Samvyo dashboard) to generate a session token
  2. The client-side application requests this token from your server
  3. The generated session token is then used in the SDK's init method along with the roomId

API Reference

POSThttps://api.samvyo.com/api/siteSetting/sessionToken

Body Parameters (application/json):

{
  "accessKey": "your_access_key",
  "secretAccessKey": "your_secret_access_key",
  "roomId": "your_room_id"
}

Response (application/json):

{
  "success": true,
  "message": "Session token generated successfully", 
  "sessionToken": "your_generated_session_token"
}
  • success: Boolean indicating if the request was successful.
  • message: A message describing the result. If an active session exists for the roomId, the message will be "Active session exists for this roomId". Otherwise, it will be "Session token generated successfully".
  • sessionToken: The generated session token to be used for SDK initialization.

Description:
This endpoint generates a session token for a given room. You must provide your access key, secret access key, and the room ID. If an active session already exists for the specified roomId, the endpoint will return the existing session token with a message indicating the active session.

Authorization:
No additional authorization header is required for this endpoint; credentials are passed in the body.

API Endpoint to Create Session Token

In your application server, implement the following API endpoint to create a session token:

Sample Code

const express = require("express");
const axios = require("axios");
const app = express();
const serverUrl = "https://api.samvyo.com";
 
app.use(express.json());
 
app.post("/api/create-session-token", async (req, res) => {
  const { roomId } = req.body;
  try {
    const response = await axios.post(
      `${serverUrl}/api/siteSetting/sessionToken`,
      {
        accessKey,
        secretAccessKey,
        roomId,
      }
    );
 
    if (response.data.success) {
      return res.status(200).send({
        message: "Session token fetched successfully",
        sessionToken: response.data.sessionToken,
      });
    }
 
    return res.status(400).send({
      message: "Failed to fetch session token",
    });
  } catch (error) {
    console.error("Error creating session token:", error);
    console.log("Error message", error.message);
    return res.status(500).json({ error: "Internal Server Error" });
  }
});;

Request From Client Side to Fetch Session Token

To use the session token in your client side application, make a request to your backend API to fetch the session token. Here's an example of how you might implement this in your client side code:

async function fetchSessionToken(roomId) {
  try {
    const response = await fetch("/api/create-session-token", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ roomId }),
    });
 
    const data = await response.json();
 
    if (response.status === 200) {
      console.log("Session Token:", data.sessionToken);
      return data.sessionToken; // Use this token to join the call
    } else {
      console.error("Error fetching session token:", data.message);
    }
  } catch (error) {
    console.error("Error:", error);
  }
}

Summary

The session token is a critical component in the Samvyo SDK integration process. It must be generated and passed during the initialization phase to establish a secure connection. The flow involves:

  1. Setting up an API endpoint in your server to generate session tokens
  2. Making a request from your client application to fetch the token
  3. Using the token along with the roomId in the SDK's init method

This setup ensures secure and authenticated communication between your application and the Samvyo server, allowing you to create and manage rooms effectively.