Calls SDKs JavaScript v1
Calls SDKs JavaScript
Calls SDKs
JavaScript
Version 1

Make your first call

Copy link

Sendbird Calls for JavaScript enables real-time voice and video calls for 1-to-1 calls or group calls among users within your Sendbird-integrated app. Our development kit can initialize, configure, and build voice and video calling functionality to your web apps.

Sendbird Calls supports both Direct call and Group call. Follow the guide below to make your 1-to-1 call or start a group call from scratch.

Note: For a more detailed guide on building voice and video calling functionality in your app, see our JavaScript video chat tutorial.


Requirements

Copy link

The minimum requirements for Calls SDK for JavaScript are the following.

  • Node
  • npm or yarn
  • WebRTC API supported browsers

Get started

Copy link

You can start making a 1-to-1 call with Direct call or create a room to start a group call with Group call by installing Sendbird Calls for JavaScript.

Step 1 Install the SDK

Copy link

You can install Calls for JavaScript through either npm or yarn.

  1. Install sendbird-calls dependency to the package.json file in your project.
# npm
npm install sendbird-calls

#yarn
yarn add sendbird-calls

Import the Calls SDK in ES6 module as shown below.

import SendBirdCall from 'sendbird-calls';

SendBirdCall.init(APP_ID)
//...

Note: If you are using TypeScript, set --esModuleInterop option to true for default imports or use import * as SendBirdCall from 'sendbird-calls'.

Or add the following code in the header to install and initialize the Calls SDK.

<script type="text/javascript" src="SendBirdCall.min.js"></script>
<script type="text/javascript">
    SendBirdCall.init(APP_ID)
</script>

Step 2 Request to access media device

Copy link

The Calls SDK requires access permissions. To allow the Calls SDK to access microphone and camera, call the SendBirdCall.useMedia() function. This allows the user to retrieve a list of available media devices or to retrieve any actual media streams.

Note: If the SendBirdCall.useMedia() function isn't called before making or receiving the first call using your JavaScript application in a browser, the browser might prompt the user to grant microphone and camera access permissions.

Step 3 Initialize the Calls SDK

Copy link

To integrate and run Sendbird Calls in your application, you need to initialize first. Initialize the SendBirdCall instance by using the Application ID of your Sendbird application, which can be found on Sendbird Dashboard after creating an application. If the instance is initialized with a different Application ID, all existing call-related data in a client app is cleared and the SendBirdCall instance is initialized again with the new Application ID.

Note: Each Sendbird application can be integrated with a single client app. Within the same application, users can communicate with each other across all platforms, whether they are on mobile devices or on the web.

// Initialize the SendBirdCall instance to use APIs in your app.
SendBirdCall.init(APP_ID);

Step 4 Authenticate a user

Copy link

To make and receive a 1-to-1 call or start a group call, authenticate a user to the Sendbird server by using their user ID through the authenticate() method. To receive calls, the SendBirdCall instance should be connected with the Sendbird server. Connect socket by using the SendBirdCall.connectWebSocket() method after a user’s authentication has been completed.

// The USER_ID below should be unique to your Sendbird application.
const authOption = { userId: USER_ID, accessToken: ACCESS_TOKEN };

SendBirdCall.authenticate(authOption, (result, error) => {
    if (error) {
        // Handle authentication failure.
    } else {
        // The user has been successfully authenticated and is connected to the Sendbird server.
        //...
    }
});

// Establishing websocket connection.
SendBirdCall.connectWebSocket()
    .then(/* Succeeded to connect */)
    .catch(/* Failed to connect */);

After authenticating a user, you can continue to either make a 1-to-1 call with Direct call or start a group call with Group call. Skip to Step 8 if you wish to start a group call.

Note: You can implement both the Chat and Calls SDKs to your app. The two SDKs can work on the same Sendbird application and share users. In this case, you can allow Calls to retrieve a list of users in the client app by using the Chat SDK’s method or Chat API.


Make 1-to-1 call

Copy link

Step 5 Add an event handler

Copy link

Sendbird Calls provides the SendBirdCallListener event handler for events related to Direct call. It is used to manage device specific events such as incoming calls.

  1. Add SendBirdCallListener by using the SendBirdCall.addListener() method. You can add this listener before SendBirdCall.authentication().
// The UNIQUE_HANDLER_ID below is a unique user-defined ID for a specific event handler.
SendBirdCall.addListener(UNIQUE_HANDLER_ID, {
    onRinging: (call) => {
        //...
    }
});

Note: If a SendBirdCallListener event handler isn’t registered, a user can't receive an onRinging callback event. Thus, this handler should be added when you initialize the app.

Step 6 Make a call

Copy link

You are now ready to make your first 1-to-1 call. To make a call, provide the callee’s user ID into the SendBirdCall.dial() method. To choose initial call configuration such as audio or video capabilities, video settings, and mute settings, use the CallOption object.

After making a call, add the call-specific DirectCallListener event handler to the call object. It allows the callee's app to respond to events happening during the call through its callback methods.

const dialParams = {
    userId: CALLEE_ID,
    isVideoCall: true,
    callOption: {
        localMediaView: document.getElementById('local_video_element_id'),
        remoteMediaView: document.getElementById('remote_video_element_id'),
        audioEnabled: true,
        videoEnabled: true
    }
};


const call = SendBirdCall.dial(dialParams, (call, error) => {
    if (error) {
        // Dialing failed.
    }

    // Dialing succeeded.
});

call.onEstablished = (call) => {
    //...
};

call.onConnected = (call) => {
    //...
};

call.onEnded = (call) => {
    //...
};

call.onRemoteAudioSettingsChanged = (call) => {
    //...
};

call.onRemoteVideoSettingsChanged = (call) => {
    //...
};

A media viewer is a HTMLMediaElement such as <audio> and <video> to display media stream. The remoteMediaView accessor is required for the remote media stream to be displayed. It is also recommended to set the value of a media viewer's autoplay property to true.

<video id="remote_video_element_id" autoplay>

Likewise, localMediaView is required for the local media stream to be displayed. And it is also recommended to set the values of a media viewer's autoplay and muted properties to true.

<video id="local_video_element_id" autoplay muted>

Note: Media viewers can also be set using the call.setLocalMediaView() or call.setRemoteMediaView() method.

// Setting media viewers lazily.
call.setLocalMediaView(document.getElementById('local_video_element_id'));
call.setRemoteMediaView(document.getElementById('remote_video_element_id'));

Step 7 Receive a call

Copy link

You can accept or decline an incoming call. To accept an incoming call, use directCall.accept(). To decline the call, use the directCall.end() method. When you accept the call, a media session is automatically established by the Calls SDK.

Before accepting the call, add the call-specific DirectCallListener event handler to the call object. It allows the callee's app to react to events happening during the call through its callback methods.

SendBirdCall.addListener(UNIQUE_HANDLER_ID, {
    onRinging: (call) => {
        call.onEstablished = (call) => {
            //...
        };

        call.onConnected = (call) => {
            //...
        };

        call.onEnded = (call) => {
            //...
        };

        call.onRemoteAudioSettingsChanged = (call) => {
            //...
        };

        call.onRemoteVideoSettingsChanged = (call) => {
            //...
        };

        const acceptParams = {
            callOption: {
                localMediaView: document.getElementById('local_video_element_id'),
                remoteMediaView: document.getElementById('remote_video_element_id'),
                audioEnabled: true,
                videoEnabled: true
            }
        };

        call.accept(acceptParams);
    }
});

Note: If media viewer elements have been set by the call.setLocalMediaView() and call.setRemoteMediaView() methods, make sure that the same media viewers are set in acceptParam’s callOption. If not, they are overridden during executing the call.accept() method.

The callee’s app receives an incoming call through the connection with the Sendbird server established by the SendBirdCall.connectWebSocket() method.


Start a group call

Copy link

Step 8 Create a room

Copy link

When creating your first room for a group call, you can choose either a room that supports up to six participants with video or a room that supports up to 100 participants with audio. When the room is created, ROOM_ID is generated.

const roomParams = {
    roomType: SendBirdCall.RoomType.SMALL_ROOM_FOR_VIDEO
};

SendBirdCall.createRoom(roomParams)
    .then(room => {
        // Room has been created successfully.
    }).catch(e => {
        // Failed to create a room.
    });

List of properties

Copy link
Property nameTypeDescription

type

RoomType

Specifies the type of the room. Valid values are the following.
- SMALL_ROOM_FOR_VIDEO: type of a room that supports audio and video, and can have up to six participants.
- LARGE_ROOM_FOR_AUDIO_ONLY: type of a room that only supports audio, and can have up to 100 participants.

Step 9 Enter a room

Copy link

You can now enter a room and start your first group call. When you enter a room, a participant is created with a unique participant ID to represent the user in the room.

To enter a room, you must first acquire the room instance from the Sendbird server with the room ID. To fetch the most up-to-date room instance from the Sendbird server, use the SendBirdCall.fetchRoomById() method. Also, you can use the SendBirdCall.getCachedRoomById() method that returns the most recently cached room instance from Sendbird Calls SDK.

SendBirdCall.fetchRoomById(ROOM_ID)
    .then(room => {
        // `room` with the identifier `ROOM_ID` is fetched from the Sendbird Server.
    })
    .catch(e => {
        // Handle error
    });

// Returns the most recently cached ‘room’ with the identifier `ROOM_ID` from the SDK.
// If there is no such room with the given identifier, `undefined` is returned.

Note: A user can enter the room using multiple devices or browser tabs. Entering from each device or browser tab creates a new participant.

Once the room is retrieved, call the enter() method to enter the room.

const enterParams = {
    videoEnabled: true,
    audioEnabled: true
}

room.enter(enterParams)
    .then(() => {
        // User has successfully entered `room`.
    })
    .catch(e => {
        // Handle error.
    });

Large room for audio only

Copy link

For the LARGE_ROOM_FOR_AUDIO_ONLY room type, call the enter() method to enter and set the HTMLAudioElement for the room to stream audio.

const roomParams = {
    roomType: SendBirdCall.RoomType.LARGE_ROOM_FOR_AUDIO_ONLY
};

const enterParams = {
  videoEnabled: true,
  audioEnabled: true
}

SendBirdCall.createRoom(roomParams)
    .then(room => {
        room.enter(enterParams)
            .then(() => {
                // Set HTMLAudioElement to set audio for a large room.
                room.setAudioForLargeRoom(AUDIO_VIEW);
            })
            .catch(e => {
                // Handle error.
            })
    }).catch(e => {
        // Failed to create a room.
    });

List of methods

Copy link
MethodDescription

setAudioForLargeRoom()

Sets audio for a large room.

Note: Share the room ID with other users for them to enter the room from the client app.