Connecting to a Session

This page covers the full connection lifecycle for a Cordova video session: enumerating media devices, joining a room, rendering local and remote video views, handling reconnection, and gracefully disconnecting when the session ends.

Media Devices

Before joining a room you can enumerate the audio and video devices available on the device. This lets you build a device-selection UI or verify that the required hardware is present.

Get All Audio / Video Devices

getDevices() returns a list of all microphones and cameras connected to the device, along with their device IDs. Use this list to populate a device-picker or to pick a specific device ID for the stream configuration.

window.EnxRtc.getDevices(
    function(data) {
        console.log("Available devices:", JSON.stringify(data.data));
        // data.data is an array of { deviceId, label, kind } objects
    },
    function(err) {
        console.error("getDevices failed:", JSON.stringify(err));
    }
);

Get the Currently Selected Device

getSelectedDevice() returns the audio/video device currently in use for the published stream. Call this after joining to confirm which device is active.

window.EnxRtc.getSelectedDevice(
    function(data) {
        console.log("Selected device:", JSON.stringify(data.data));
    },
    function(err) {
        console.error("getSelectedDevice failed:", JSON.stringify(err));
    }
);
Joining a Room

Connecting to an EnableX room normally involves multiple sequential steps: initialise the room, check media access, publish the local stream, and confirm publication. joinRoom() compresses all of these steps into a single call so you can get the session running quickly.

The method accepts three JSON configuration objects: one for the local media stream, one for the room behaviour, and then success/error callbacks. After joinRoom() returns successfully, call initLocalView() and initRemoteView() to render video.

Parameters

ParameterTypeDescription
tokenStringSigned JWT issued by your server for this user
publishStreamInfoJSON ObjectLocal stream configuration (see below)
roomInfoJSON ObjectRoom behaviour options (see below)
successCallbackFunctionCalled when the join request is accepted
errorCallbackFunctionCalled if the join request fails

publishStreamInfo Options

KeyTypeDescription
audioBooleanEnable audio track in the local stream
videoBooleanEnable video track in the local stream
dataBooleanEnable data channel for chat and signaling
audioOnlyModeBooleanJoin in audio-only mode; no video published or received
audioMutedBooleanStart with microphone muted
videoMutedBooleanStart with camera muted
maxVideoLayersNumberMaximum simulcast video layers to publish
nameStringDisplay name shown to other participants

roomInfo Options

KeyTypeDescription
activeviewsStringView layout mode, e.g. "list"
allow_reconnectBooleanAllow automatic reconnection on network drop
number_of_attemptsNumberMaximum reconnection attempts
timeout_intervalNumberSeconds between reconnection attempts
playerConfigurationJSON ObjectPlayer UI controls and avatar sizing
forceTurnBooleanForce all media through TURN relay
chat_onlyBooleanJoin as a chat-only participant (no media)

Event Listeners

EventFired ToDescription
onRoomConnectedSelfSuccessfully connected to the room
onRoomErrorSelfConnection to the room failed
onRoomDisConnectedSelfDisconnected from the room
onUserConnectedAll othersA new participant joined the room
onUserDisConnectedAll othersA participant left the room
onPublishedStreamSelfLocal stream successfully published
onUnPublishedStreamSelfLocal stream unpublished from the room
onConnectionLostSelfNetwork connection lost
onConnectionInterruptedSelfConnection interrupted (e.g. WiFi to 4G switch)
onUserReconnectSuccessSelfReconnected successfully after a drop
onReconnectSelfReconnection attempt in progress
// Stream configuration
var streamOpt = {
    audio: true,
    video: true,
    data: true,
    audioOnlyMode: false,
    audioMuted: false,
    videoMuted: false,
    maxVideoLayers: 1,
    name: "John"
};

// Player UI controls embedded in roomInfo
var playerConfiguration = {
    audiomute: true,
    videomute: true,
    bandwidth: true,
    screenshot: true,
    avatar: true,
    iconHeight: 30,
    iconWidth: 30,
    avatarHeight: 200,
    avatarWidth: 200
};

// Room behaviour options
var roomOpt = {
    activeviews: "list",
    allow_reconnect: true,
    number_of_attempts: 3,
    timeout_interval: 15,
    playerConfiguration: playerConfiguration,
    forceTurn: false,
    chat_only: false
};

// Register listeners before joining
window.EnxRtc.addEventListner("onRoomConnected", function(data) {
    console.log("Connected:", JSON.stringify(data.data));
    initLocalView();   // Render local video
    initRemoteView();  // Render remote video
});

window.EnxRtc.addEventListner("onRoomError", function(data) {
    console.error("Room error:", JSON.stringify(data.data));
});

window.EnxRtc.addEventListner("onUserConnected", function(data) {
    console.log("User joined:", JSON.stringify(data.data));
});

window.EnxRtc.addEventListner("onUserDisConnected", function(data) {
    console.log("User left:", JSON.stringify(data.data));
});

// Join the room
window.EnxRtc.joinRoom(
    result.token,
    streamOpt,
    roomOpt,
    function(data) { console.log("Join OK:", JSON.stringify(data.data)); },
    function(err)  { console.error("Join failed:", JSON.stringify(err)); }
);
Rendering Video Views

After the room is connected, you need to create two native view containers: one for the local camera feed and one for remote participants. Call these methods inside your onRoomConnected listener.

Initialise the Local View

initLocalView() creates and positions the native view that renders your own camera stream. Pass a localviewOptions object to control the size and position of the view.

KeyTypeDescription
heightNumberView height in points
widthNumberView width in points
margin_topNumberTop margin offset
margin_leftNumberLeft margin offset
margin_rightNumberRight margin offset
margin_bottomNumberBottom margin offset
positionStringPositional hint (e.g. "top")
function initLocalView() {
    var localViewOptions = {
        height: 130,
        width: 100,
        margin_top: 50,
        margin_left: 0,
        margin_right: 15,
        margin_bottom: 10,
        position: "top"
    };

    window.EnxRtc.initLocalView(
        localViewOptions,
        function(data) { console.log("Local view ready:", JSON.stringify(data.data)); },
        function(err)  { console.error("Local view error:", JSON.stringify(err)); }
    );
}

Initialise the Remote View

initRemoteView() creates and positions the native view that renders remote participants' streams. Call this alongside initLocalView() after the room connects.

function initRemoteView() {
    var remoteViewOptions = {
        height: 600,
        width: 800,
        margin_top: 100,
        margin_left: 0,
        margin_right: 15,
        margin_bottom: 10,
        position: "center"
    };

    window.EnxRtc.initRemoteView(
        remoteViewOptions,
        function(data) { console.log("Remote view ready:", JSON.stringify(data.data)); },
        function(err)  { console.error("Remote view error:", JSON.stringify(err)); }
    );
}
Reconnection

If a user's network drops or switches (e.g. from WiFi to mobile data), the SDK fires a sequence of reconnection events. You can listen to these to show UI feedback and restore the session state when the connection comes back.

// Network connection lost
window.EnxRtc.addEventListner("onConnectionLost", function(data) {
    console.warn("Connection lost. Attempting to reconnect...");
    showReconnectingBanner();
});

// A reconnection attempt is underway
window.EnxRtc.addEventListner("onReconnect", function(data) {
    console.log("Reconnecting:", JSON.stringify(data.data));
});

// Reconnection succeeded
window.EnxRtc.addEventListner("onUserReconnectSuccess", function(data) {
    console.log("Reconnected successfully:", JSON.stringify(data.data));
    hideReconnectingBanner();
});
Disconnecting

Call disconnect() to close the session for the local user. The endpoint immediately loses media and signaling. All other participants in the room receive an onUserDisConnected notification.

EventFired ToDescription
onRoomDisConnectedSelfConfirms disconnection for the local user
onUserDisConnectedAll othersNotifies remaining participants that this user left
// Disconnect the local user
window.EnxRtc.disconnect();

// Confirmation for the disconnected user
window.EnxRtc.addEventListner("onRoomDisConnected", function(data) {
    console.log("You have left the room:", JSON.stringify(data.data));
    navigateToHomeScreen();
});

// Notification to everyone else
window.EnxRtc.addEventListner("onUserDisConnected", function(data) {
    console.log("A participant left:", JSON.stringify(data.data));
    removeParticipantFromUI(data.data.clientId);
});