Room Connection
The Cordova SDK provides the following methods:
- joinRoom(): To join a room quickly.
- localviewOptions(): To initiate a local view.
- initRemoteView(): To initiate a remote view.
- disconnect(): To close the session and disconnect the client endpoint from the room.
Join a Room with a Stream
Connecting and joining a room is a complex chain of events. You must ensure the success status of the previous event to proceed to the next event. A typical process to connect to a room involves the following steps:
- Initiate a room and connect to it.
- Initiate streaming after connecting to a room, which requires you to check the media accessibility.
- Publish the local stream.
- Check if the stream is successfully published.
To successfully join a room, you need to ensure the success of each step before proceeding to the next step, thus making it a complex process. The joinRoom()
method allows you to quickly join a room and get started without following every step of this procedure.
- Method:
JoinRoom(token,publishStreamInfo,roomInfo,success,error)
- Parameters:
token
: String. This is an encoded token string received from the Enx application server.publishStreamInfo
: JSONObject. Published stream information for local streams.roomInfo
: JSONObject. Room information for joining a room.successCallback
: Callable Function. Success Notification.errorCallback
: Callable Function. Error Notification.
- Event Listeners:
onRoomConnected
: When the client endpoint successfully connected to the room.onRoomDisConnected
: When the client endpoint gets disconnected from the room.onRoomError
: When the client endpoints attempt fails to connect to a room.onUserConnected
: Everyone is notified that a new user is connected to the room.onConnectionLost
: When endpoint loses network connection.onConnectionInterrupted
: When the connection is interrupted, e.g., switch from WiFi to 4G and vice versa.onUserReconnectSuccess
: When endpoint successfully gets reconnected with EnableX.onReconnect
: When endpoint tries to reconnect within the given period.onPublishedStream
: The publisher is notified that its stream has been published into the room.onUnPublishedStream
: The publisher is notified that its stream is unpublished/removed from the room.
Sample Code
var streamOpt = {audio: true,video: true,data: true,audioOnlyMode: false,audioMuted: false,videoMuted: false,maxVideoLayers: 1,name: "John"};var playerConfiguration = {audiomute: true,videomute: true,bandwidth: true,screenshot: true,avatar: true,iconHeight: 30,iconWidth: 30,avatarHeight: 200,avatarWidth: 200};var roomOpt = {activeviews: "list",allow_reconnect: true,number_of_attempts: 3,timeout_interval: 15,playerConfiguration: playerConfiguration,forceTurn: false,chat_only: false,};// Connect Video Roomwindow.EnxRtc.joinRoom(result.token, streamOpt,roomOpt, function(data) {console.log( JSON.stringify(data.data));initLocalView(); // To init local ViewinitRemoteView(); // To init Remote View}, function (err) {});// Event Listenerswindow.EnxRtc.addEventListner("onRoomConnected", function(data) {console.log( JSON.stringify(data.data));});window.EnxRtc.addEventListner("onRoomError", function(data) {console.log( JSON.stringify(data.data));initLocalView(); // To init local ViewinitRemoteView(); // To init Remote View});window.EnxRtc.addEventListner("onEventError", function(data) {console.log(JSON.stringify(data.data));});window.EnxRtc.addEventListner("onUserConnected", function(data) {console.log(JSON.stringify(data.data));});window.EnxRtc.addEventListner("onUserDisConnected", function(data) {console.log( JSON.stringify(data.data));});
Initiate a Local and Remote View
Initiate a Local View
The initLocalView()
method is used to show local streams. Users must initiate local views in the success of the room-connected event.
- Method:
initLocalView( localviewOptions, successCallback, errorCallback )
- Parameters:
localviewOptions
: JSON Object. View Settings.height
: Number. View Height.width
: Number. View Width.margin_top
: Number. Top Margin. Not Applicable.margin_bottom
: Number. Bottom Margin. Not Applicable.margin_left
: Number. Left Margin. Not Applicable.margin_right
: Number. Right Margin. Not Applicable.position
: String. Not Applicable.
successCallback
: Callable Function. Success Notification.errorCallback
: Callable Function. Error Notification.
Sample Code
// To Init Local Viewvar initLocalViewOptions = {height: 130,width: 100,margin_top: 50,margin_left: 0,margin_right: 15,margin_bottom: 10,position: "top"};window.EnxRtc.initLocalView(initLocalViewOptions, function(data) {console.log(JSON.stringify(data.data));}, function(err) {console.log('Uh oh… error' + JSON.stringify(err));});
Initiate a Remote View
The initRemoteView()
method is used to initiate a remote view. Users must initiate remote views in the success of the room-connected event.
- Method:
initRemoteView(remoteviewOptions, successCallback, errorCallback )
- Parameters:
remoteviewOptions
: JSON Object. View Settings.height
: Number. View Height.width
: Number. View Width.margin_top
: Number. Top Margin. Not Applicable.margin_bottom
: Number. Bottom Margin. Not Applicable.margin_left
: Number. Left Margin. Not Applicable.margin_right
: Number. Right Margin. Not Applicable.position
: String. Not Applicable.
successCallback
: Callable Function. Success Notification.errorCallback
: Callable Function. Error Notification.
Sample Code
// To init Remote Viewvar initRemoteViewOptions = {height: 600,width: 800,margin_top: 100,margin_left: 0,margin_right: 15,margin_bottom: 10,position: "center"};window.EnxRtc.initRemoteView(initRemoteViewOptions, function(data) {console.log( JSON.stringify(data.data));}, function (err) {console.log(JSON.stringify(err));});
Disconnect from a room
A client endpoint can disconnect itself from the room to close its session. A disconnected endpoint will lose media and signaling socket immediately.
- Method:
disconnect()
- Event Listeners:
onRoomDisconnected
: To the user who gets disconnected.onUserDisconnected
: To all other connected users.
Sample Code
window.EnxRtc.disconnect();// Event Listener to self- Disconnectedwindow.EnxRtc.addEventListner("onRoomDisConnected", function(data) {console.log(JSON.stringify(data.data));});// Event Listener for otheres- Disconnectedwindow.EnxRtc.addEventListner("onUserDisConnected", function(data) {console.log(JSON.stringify(data.data));};