Connecting to a session
Once you have a session ID, and initialized a session object using it, the next step is to connect to the session.
Learn how to connect to sessions below by selecting your platform/language of choice:
When you add an OTSession component it automatically connects to the Vonage Video API session.
<OTSession
applicationId="your-application-ID"
sessionId="your-session-id"
token="your-session-token"
>
<OTPublisher/>
<OTSubscriber/>
</OTSession>
Replace your-application-ID, your-session-id, and your-session-token with your API key, a session ID, and a token for the session.
Note that you add the OTPublisher and OTSubscriber components and children of the OTSession component.
You can pass an error and sessionConnected event handlers in the OTSession component. The error event handler is called if the client fails to connect to the session. And the sessionConnected event handler is called when the client connects to the session:
<OTSession
applicationId="your-application-ID"
sessionId="your-session-id"
token="your-session-token"
eventHandlers={{
error: event => {
console.log('error', event);
},
sessionConnected: event => {
console.log('session connected', event);
},
}}
>
<OTPublisher style={{ width: 100, height: 100 }}/>
<OTSubscriber style={{ width: 100, height: 100 }} />
</OTSession>
Call the connect() method, passing in a token and a completion handler function:
var session = OT.initSession(appID, sessionId);
session.connect(token, function(error) {
if (error) {
console.log("Error connecting: ", error.name, error.message);
} else {
console.log("Connected to the session.");
}
});
An error object is passed into the completion handler of the connect event when the client fails to connect to the session. Otherwise, no error object is passed in, indicating that the client connected successfully to the session.
The Session object also dispatches a sessionConnected event when the client connects to the session. And the OT object dispatches an exception event when there is an error in connecting. However, it is simpler to check for success in connecting by passing a completion handler into the connect() method, as the last parameter.
Call the Session.connect(token) method, passing in a valid token:
mSession.connect(TOKEN);
The Session.SessionListener.onConnected(Session session) method is called when the client connects to the Vonage Video session.
@Override
protected void onConnected(Session session)
// This client has connected to the session.
}
The Session.SessionListener.onError(Session session, OpentokError error) method is called when there is an error in connecting. See the documentation for the OpentokException.ErrorCode enum for descriptions of values of the code property of the error object.
@Override
public void onError(Session session, OpentokError error) {
Log.i(LOGTAG, "Exception: " + error.getMessage());
}
Call the OTSession connect(withToken:error:) method, passing in the token for the client:
var error: OTError?
session.connect(withToken: token, error: &error)
if let error = error {
print("connect failed with error: \(error)")
}
The OTSessionDelegate sessionDidConnect(_:) message is sent when the client connects to the Vonage Video session.
The OTSessionDelegate session(_:didFailWithError:) is sent when there is an error in connecting.
See the documentation for the OTSessionErrorCode enum for descriptions of values of the code property of the error object.
Monitoring the connection status (Swift)
You can get the connection status by checking the sessionConnectionStatus property of the OTSession object:
session.sessionConnectionStatus
Valid values are defined in the OTSessionConnectionStatus enum.
You can use a key-value observer to monitor this property. However, the OTSessionDelegate sessionDidConnect(_:) and OTSessionDelegate sessionDidDisconnect(_:) messages are sent to the session's delegate when the session connects and disconnects.
Call the [OTSession connectWithToken:error:] method, passing in the token for the client:
OTError* error = nil;
[session connectWithToken:kToken error:&error];
if (error) {
NSLog(@"connect failed with error: (%@)", error);
}
The [OTSessionDelegate session:didConnect] message is sent when the client connects to the Vonage Video session.
The OTSessionDelegate session:didFailWithError:] is sent when there is an error in connecting. See the documentation for the OTSessionErrorCode enum for descriptions of values of the code property of the error object.
Monitoring the connection status (Objective C)
You can get the connection status by checking the sessionConnectionStatus property of the OTSession object:
session.sessionConnectionStatus
Valid values are defined in the OTSessionConnectionStatus enum.
You can use a key-value observer to monitor this property. However, the [OTSessionDelegate sessionDidConnect:] and [OTSessionDelegate sessionDidDisconnect:] messages are sent to the session's delegate when the session connects and disconnects.
Call the Session.connect(token) method, passing in a valid OpenTok token:
session.Connect(TOKEN);
The Session.Connected event is sent when the client connects to the OpenTok session.
session.ConnectionCreated += Session_ConnectionCreated;
private void Session_Connected(object sender, EventArgs e)
{
Console.WriteLine("Session connected connection id:" + session.Connection.Id);
}
The Session.Error event is sent when there is an error in connecting:
session.Error += Session_Error;
private void Session_Error(object sender, Session.ErrorEventArgs e)
{
Console.WriteLine("Session error:" + e.ErrorCode);
}
See the documentation for the OpenTok.ErrorCode enum for descriptions of values of the code property of the error object.
Call the otc_session_connect() function:
otc_session_connect(session, TOKEN);
This function connects the client to the Vonage Video session. It takes two arguments:
- The
otc_sessionstructure instance. - The Vonage Video token string.
Upon successfully connecting, the on_connected callback function of the otc_session_callbacks struct is called. Upon error, the on_error callback function of the otc_session_callbacks struct is called.