Manage sessions


The code below demonstrates how to authenticate, initialize and join, and leave sessions.

Authenticate

Generate a token to authenticate a session.

import { KJUR } from 'jsrsasign';

function generateVideoToken(
  sdkKey: string,
  sdkSecret: string,
  topic: string,
  passWord = '',
  sessionKey = '',
  userIdentity = '',
) {
  let signature = '';
  try {
    const iat = Math.round(new Date().getTime() / 1000);
    const exp = iat + 60 * 60 * 2;

    // Header
    const oHeader = { alg: 'HS256', typ: 'JWT' };
    // Payload
    const oPayload = {
      app_key: sdkKey,
      iat,
      exp,
      tpc: topic,
      pwd: passWord,
      user_identity: userIdentity,
      session_key: sessionKey,
      // topic
    };
    // Sign JWT, password=616161
    const sHeader = JSON.stringify(oHeader);
    const sPayload = JSON.stringify(oPayload);
    signature = KJUR.jws.JWS.sign('HS256', sHeader, sPayload, sdkSecret);
  } catch (e) {
    console.error(e);
  }
  return signature;
}

Initialize and join a session

To launch a session, first create a client through the createClient method. Initialize the client with the client.init method, then join a user through the client.join method.

The client.join method requires a topic, signature, userName, and password.

The signature parameter is the signature created by the generate signature function. This is required. This signature should be requested and generated on a backend service, then passed into the client.join method. See Generate Signature for details.

The code below shows how to initialize and join a session.

const client = ZoomVideo.createClient();
client.init();
client.join(
  topic,
  signature,
  userName,
  password,
).then(() => {
  console.log('Join meeting success');
}).catch((error) => {
  console.error(error);
});

When the SDK connects to Zoom servers, use the connection-change event to get the session details, such as if the session has Connected.

import {
  ConnectionState
} from '@zoom/videosdk';

client.on('connection-change', (payload) => {
  if (payload.state === ConnectionState.Connected) {
    // handle connected
  } else if(payload.state === ConnectionState.Reconnecting){
    // handle failover
  }
});

Leave the session

Use the client.leave method to leave the session.

client.leave();

End the session

Use client.leave(true) to end the session if the client is the host.

if(client.isHost()){
  client.leave(true);
}

Add more features

Follow the Essential Guides to manage video and audio streams and start building your app.