Joining a Session
In this guide you will learn to connect your application to a video session.
Prerequisite
You will need a valid Vonage Video API account, if you don't have one you can sign up for a free trial.
You should also ensure that you have created a session and generated a token for all users involved.
Initializing a Session object
Before you can connect to a session, you will need to instantiate a session object using any of the client SDKs available.
Instantiate a Session object by calling the OT.initSession() method with your application ID and the appropriate session ID:
// Replace with your application ID and session ID:
var session = OT.initSession(applicationId, sessionId);
The OT.initSession() method returns a Session object, through which subsequent API calls take place.
Note that calling the OT.initSession() method does not create a session; it creates a JavaScript Session object, which represents an existing session. You can create a session using the server-side SDK. See Creating a session.
If the user's browser does not support WebRTC, the call to OT.initSession() results in the page displaying a message to the user. To check for WebRTC support and prevent this message from being displayed, you can call the OT.checkSystemRequirements() method before calling OT.initSession():
if (OT.checkSystemRequirements() == 1) {
var session = OT.initSession(applicationId, sessionId);
} else {
// The client does not support WebRTC.
// You can display your own message.
}
Instantiate a Session.Builder object by calling the Session.Builder() constructor, passing in the appropriate Android application context, your Vonage Video application ID, and a session ID. Then call the build() method of the Session.Builder object to create a Session object:
mSession = new Session.Builder(context, APPLICATION_ID, SESSION_ID)
.build();
Note that calling the Session.Builder.build() method does not create a Vonage Video session; it creates the Java Session object, which represents an existing Vonage Video session. You create a Vonage Video session using the Vonage Video server-side libraries. See Creating a Vonage Video session.
Add a listener object for basic session-related events by calling the setSessionListener(Session.SessionListener listener) method of the Session object:
mSession.setSessionListener(this);
Implement the methods of the Session.SessionListener interface in the object you specify as the event listener object. These methods are called when session-related events occur.
Instantiate an OTSession object by calling the OTSession init(apiKey:sessionId:delegate:) method with your application ID and the appropriate session ID:
// Replace kApplicationId with your application ID:
// Replace kSessionId with a session ID:
session = OTSession(apiKey: kApplicationId, sessionId: kSessionId, delegate: self)
Note that calling the OTSession init(apiKey: sessionId: delegate:) method does not create a Vonage Video session; it creates the Swift OTSession object, which represents an existing Vonage Video session. You create a Vonage Video session using the Vonage Video server-side libraries.
See Creating a Vonage Video session.
Implement the methods of the OTSessionDelegate protocol in the object you specify as the delegate object. These methods are called when session-related events occur.
Before you can connect to a session, instantiate an OTSession object by calling the [OTSession initWithApiKey: sessionId: delegate:] method with your application ID and the appropriate session ID:
// Replace kApplicationId with your application ID":
// Replace kSessionId with an OpenTok session ID:
session = [[OTSession alloc] initWithApiKey:kApplicationId
sessionId:kSessionId
delegate:self];
Note that calling the [OTSession initWithApiKey:sessionId:delegate:] method does not create a Vonage Video session; it creates the Objective-C OTSession object, which represents an existing Vonage Video session. You create a Vonage Video session using the Vonage Video server-side libraries. See Creating a Vonage Video session.
Implement the methods of the OTSessionDelegate protocol in the object you specify as the delegate object. These methods are called when session-related events occur.
Instantiate a Session object by calling the Session() constructor, passing in the appropriate Windows application context, your application ID, and a Vonage Video session ID:
session = new Session(Context.Instance, APPLICATION_ID, SESSION_ID);
Note that calling the Session() constructor does not create a Vonage Video session; it creates a C# Session object, which represents an existing Vonage Video session. You create a Vonage Video session using the Vonage Video server-side libraries. See Creating a Vonage Video session.
You will want to add handlers for basic session-related events:
session.Connected += Session_Connected;
session.Disconnected += Session_Disconnected;
session.Error += Session_Error;
session.ConnectionCreated += Session_ConnectionCreated;
session.StreamReceived += Session_StreamReceived;
session.StreamDropped += Session_StreamDropped;
You will want to implement each of the callback methods. For example, this method handles ConnectionCreated event (which occurs when the client connects to the Vonage Video session):
private void Session_Connected(object sender, EventArgs e)
{
Console.WriteLine("Session connected connection id:" + session.Connection.Id);
}
Note: The Session class implements the System.IDisposable interface. Be sure to call the Dispose() method of the Session object to release their resources when you no longer need the object (for example, when the app or window is closing).
Create a structure of type otc_session_callbacks, and function pointers to callback function members. For example:
char *session_user_data = strdup("Session user data");
static void on_session_connected(otc_session *session, void *user_data) {
// You could publish a stream once you connect to the session.
}
static void on_session_stream_received(otc_session *session,
void *user_data,
const otc_stream *stream) {
// You could call otc_subscriber_new() to subscribe to this stream
// in response to this event.
}
static void on_session_stream_dropped(otc_session *session,
void *user_data,
const otc_stream *stream) {
// If you have subscribed to this stream, you should
// call otc_subscriber_delete() to delete the subscriber in response to this event.
}
static void on_disconnected(otc_session *session, void *user_data) {
// Handle the on_disconnected event.
}
static void on_session_error(otc_session *session,
void *user_data,
const char *error_string,
enum otc_session_error_code error) {
// Handle the error.
}
struct otc_session_callbacks session_callbacks = {0};
session_callbacks.user_data = session_user_data;
session_callbacks.on_connected = on_session_connected;
session_callbacks.on_stream_received = on_session_stream_received;
session_callbacks.on_stream_dropped = on_session_stream_dropped;
session_callbacks.on_disconnected = on_session_disconnected;
session_callbacks.on_error = on_session_error;
Use the user_data member of the otc_session_callbacks structure to set data you may want to reference in the callback functions. In this example, we set it to a pointer to a string object. But it could be a pointer to an instance of some other type that contains meaningful information.
The other members of the otc_session_callbacks structure are callback functions that are called when events related to the Vonage Video session occur. The previous example includes callbacks for the following:
on_connected\-- Called when theotc_session_connect()function (see below) successfully connects the instance to a Vonage Video session.on_stream_received-- Called when there is a new stream in the Vonage Video session (when another client publishes a stream to the session).on_stream_dropped-- Called when another client's stream is dropped from the Vonage Video session. This can happen when the client stops publishing the stream or if the client's network connection drops.on_disconnected-- Called when the application disconnects from the Vonage Video session (see below).on_error-- Called when an error occurs in connecting to the session. This function includes parameters for an error string and an error code that is defined by theotc_session_error_codeenum.
All callbacks will not be made on the application or main thread but on an internal thread. The application should return the callback as quickly as possible to avoid blocking the internal thread.
See otc_session_callbacks in the Vonage Video Linux SDK reference for details on each of the callback functions.
After initializing the otc_session_callbacks structure, call the otc_session_new() function, passing in your application ID string, the Vonage Video session ID string, and a pointer to the otc_session_callbacks structure:
otc_session *session = NULL;
session = otc_session_new(APPLICATION_ID, SESSION_ID, &session_callbacks);
The otc_session_new() function returns an otc_session structure, which represents a Vonage Video session.
To use advanced session settings, call the otc_session_new_with_settings(), instead of the otc_session_new() function. This function takes an settings parameter that is a pointer to an otc_session_settings struct that defines the advanced settings. For example, the following code uses the otc\_session_settings_new () function to instantiate an otc_session_settings struct and calls otc_session_settings_set_connection_events_suppressed(OTC_TRUE) to have the SDK suppress connection events, to support large interactive video sessions. It then passes the otc_session_settings struct into the otc_session_new_with_settings() function:
// Populate the session_callbacks struct, as in the previous example. Then...
otc_session_settings *session_settings = otc_session_settings_new();
otc_session_settings_set_connection_events_suppressed(session_settings, OTC_TRUE);
otc_session *session = otc_session_new_with_settings(APPLICATION_ID,
SESSION_ID,
&session_callbacks,
session_settings);
In addition to otc_session_settings_set_connection_events_suppressed(), the following functions let you set advanced settings for a session:
otc_session_settings_set_custom_ice_config()— Enables custom ICE sever configuration. This is part of the configurable TURN feature.otc_session_settings_set_ip_whitelist()— This supports the allowed IP address feature available as an add-on feature.otc_session_settings_set_proxy_url()— Sets an IP proxy URL. See the IP Proxy developer guide.
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.
Disconnect from a Session
Learn how to disconnect a user from a session.
The client disconnects from the session when you unmount the OTSession component.
To disconnect from a session, call the disconnect() method of the Session object:
session.disconnect();
To disconnect from a session, call the Session.disconnect() method.
mSession.disconnect();
Detecting when you have disconnected (Android)
The Session.SessionListener.onDisconnected(Session session) method is called when the client disconnects from the session.
@Override
public void onDisconnected(session) {
// This client has disconnected to the session.
}
If the connection to the session drops due to an error that occurs after a successful connection, the Session.SessionListener.onError(Session session, OpentokError error) method is called prior to the Session.SessionListener.onDisconnected(Session session) method. The OpentokError object passed into the Session.SessionListener.onError(Session session, OpentokError error) method describes the reason for the disconnection.
To disconnect from a session, call the OTSession disconnect(_:) method.
var error: OTError?
session.disconnect(&error)
if let error = error {
print("disconnect failed with error: \(error)")
}
Detecting when you have disconnected (Swift)
The OTSessionDelegate sessionDidDisconnect(_:) message is sent to the session's delegate when the session disconnects.
Note that sessions automatically disconnect when the app is suspended.
If the connection to the session drops due to an error that occurs after a successful connection, the OTSessionDelegate session(_:didFailWithError:) message is sent prior to the OTSessionDelegate sessionDidDisconnect(_:) message.
The OTSessionErrorCode enum defines the code property of the OTError object passed into the OTSessionDelegate session(_:didFailWithError:) message, and it describes the reason for the disconnection.
To disconnect from a session, call the [OTSession disconnect:] method.
OTError* error = nil;
[session disconnect:&error];
if (error) {
NSLog(@"disconnect failed with error: (%@)", error);
}
Detecting when you have disconnected (Objective C)
The [OTSessionDelegate sessionDidDisconnect:] message is sent to the session's delegate when the session disconnects.
Note that sessions automatically disconnect when the app is suspended.
If the connection to the session drops due to an error that occurs after a successful connection, the [OTSessionDelegate session:DidFailWithError:] message is sent prior to the [OTSessionDelegate sessionDidDisconnect:] message. The OTSessionErrorCode enum defines the code property of the OTError object passed into the [OTSessionDelegate session:DidFailWithError:] message, and it describes the reason for the disconnection.
To disconnect from a session, call the Session.Disconnect() method:
session.Disconnect();
Detecting when you have disconnected (Windows)
The Session.Disconnected message is sent when the client disconnects from the Vonage Video session.
private void Session_Disconnected(object sender, EventArgs e)
{
Console.WriteLine("Session disconnected");
}
If the connection to the session drops due to an error that occurs after a successful connection, the Session.Error event is sent prior to the Session.Disconnected event. The ErrorEventArgs object passed into the Session.Error event handler define the reason the connection dropped.
To disconnect from a session, call the otc_session_disconnect() function:
otc_session_disconnect(session);
If you will not be reconnecting to the session, you should call the otc_session_delete() and otc_destroy() functions:
otc_session_delete(session);
session = NULL;
otc_destroy();
Detecting when you have disconnected (Linux)
When the client disconnects from the OpenTok session, the on_disconnected callback function of the otc_session_callbacks struct is called.
Detecting when clients have connected and disconnected
Learn how to detect when a client connects and disconnects from a session.
The OTSession object dispatches a connectionCreated event when a new client (excluding your own) connects to the session. The OTSession object dispatches a connectionDestroyed event when other clients leave the session. These events are defined by the ConnectionEvent class, which has a connection object, which is a Connection object for the connection (created or destroyed) related to the event:
let this.connectionCount = 0;
const this.sessionEventHandlers = {
connectionCreated: function (event) {
connectionCount++;
if (event.connection.connectionId != session.connection.connectionId) {
console.log('Another client connected. ' + connectionCount + ' total.');
}
},
connectionDestroyed: function connectionDestroyedHandler(event) {
connectionCount--;
console.log('A client disconnected. ' + connectionCount + ' total.');
}
sessionConnected: function (event) {
// include your own client's connection in the count
connectionCount++;
},
};
// reference later in JSX:
<OTSession
applicationId={this.apiKey}
sessionId={this.sessionId}
token={this.token}
eventHandlers={this.sessionEventHandlers}
>
{/* ... */}
The Session object dispatches a connectionCreated event when a new client (including your own) connects to the session. The Session object dispatches a connectionDestroyed event when other clients leave the session. These events are defined by the ConnectionEvent class, which has a connection object, which is a Connection object for the connection (created or destroyed) related to the event:
var connectionCount;
session.on({
connectionCreated: function (event) {
connectionCount++;
if (event.connection.connectionId != session.connection.connectionId) {
console.log('Another client connected. ' + connectionCount + ' total.');
}
},
connectionDestroyed: function connectionDestroyedHandler(event) {
connectionCount--;
console.log('A client disconnected. ' + connectionCount + ' total.');
}
});
session.connect(token, function (error) {
if (error) {
console.log("Failed to connect.");
} else {
console.log('You have connected to the session.');
}
});
When you are connected to a session, the Session.ConnectionListener.onConnectionCreated(Session session, Connection connection) method is called when a new client (other than your own) connects to the session.
The Session.ConnectionListener.onConnectionDestroyed(Session session, Connection connection) method is called when a client (other than your own) leaves the session. The Connection object passed into the method defines the connection hat has left the session.
Add a listener object for these connection events by calling the setConnectionListener(Session.ConnectionListener listener) method of the Session object:
mSession.setSessionListener(this);
@Override
public void onConnectionCreated(Session session, Connection connection)
{
// New client connected to the session
}
@Override
public void onConnectionDestroyed(Session session, Connection connection)
{
// A client disconnected from the session
}
The OTSessionDelegate session(_: connectionCreated:) message is sent to the session's delegate when another client connects to the session (and for each client connected to the session when you connect).
The OTSessionDelegate session(_: connectionDestroyed:) message is sent to the session's delegate when another client disconnects from the session.
The [OTSessionDelegate session:connectionCreated:] message is sent to the session's delegate when another client connects to the session (and for each client connected to the session when you connect).
The [OTSessionDelegate session:connectionDestroyed:] message is sent to the session's delegate when another client disconnects from the session.
When you are connected to a session, the Session.ConnectionCreated event is sent when a new client (other than your own) connects to the session. The ConnectionEventArgs object passed into the event listener defines the connection hat has left the session:
session.ConnectionCreated += Session_ConnectionCreated;
private void Session_ConnectionCreated(object sender, EventArgs e)
{
// Another client connected to the session.
}
The Session.ConnectionDropped event is sent when a client (other than your own) leaves the session. The ConnectionEventArgs object passed into the event listener defines the connection that has left the session.
session.ConnectionCreated += Session_ConnectionDropped;
private void Session_ConnectionDropped(object sender, EventArgs e)
{
// Another client disconnected from the session.
}
Once you have connected to a session, the on_connection_created callback function of the otc_session_callbacks struct is called when a new client (other than your own) connects to the session. The connection parameter of that function is a pointer to an instance of an otc_connection struct corresponding to the client connecting to the session.
The on_connection_dropped callback function of the otc_session_callbacks struct is called when a client (other than your own) disconnects from the session. The connection parameter of that function is a pointer to the otc_connection struct corresponding to the client disconnecting from the session.
Detecting when you have disconnected
You setup an EventListner to execute a function if a user is disconnected from a session.
For example, the function can notify the user whenever they loose connection and are no longer connected to the session.
When your client disconnects from a session, the OTSession component dispatches a sessionDisconnected event:
<OTSession
applicationId="your-api-key"
sessionId="your-session-id"
token="your-session-token"
eventHandlers={{
sessionDisconnected: event => {
console.log('disconnected', event);
},
connected: event => {
console.log('subscriber connected', event);
},
}}
>
<OTPublisher style={{ width: 100, height: 100 }}/>
<OTSubscriber style={{ width: 100, height: 100 }} />
</OTSession>
When your client disconnects from a session, the Session object dispatches a sessionDisconnected event:
session.on("sessionDisconnected", function (event) {
// The event is defined by the SessionDisconnectEvent class
if (event.reason == "networkDisconnected") {
alert("Your network connection terminated.")
}
});
session.connect(token);
The reason property of the event is a string that describes why the session disconnected. For example, the previous example notifies the user if they were disconnected due to the network connection terminating.
For details, see SessionDisconnectEvent.
Automatic reconnection
Clients will attempt to automatically reconnect to a session they disconnect unexpectedly (for example, due to a drop in network connectivity).
You do not need to add any code to have the clients reconnect automatically, unless you want to respond to events dispatched when your client disconnects and reconnects.
Clients will attempt to automatically reconnect to a session they disconnect unexpectedly (for example, due to a drop in network connectivity). You do not need to add any code to have the clients reconnect automatically, unless you want to respond to events dispatched when your client disconnects and reconnects.
When the connection is dropped and the client tries to reconnect, the OTSession object dispatches a sessionReconnecting event. When the connection is restored, the Session object dispatches a sessionReconnected. If the client cannot restore the connection, the client disconnects from the session, and the Session object dispatches the sessionDisconnected.
In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states:
<OTSession
applicationId={this.apiKey}
sessionId={this.sessionId}
token={this.token}
eventHandlers={{
sessionReconnecting: event => {
// Display a user interface notification.
},
sessionReconnected: event => {
// Adjust user interface.
},
sessionDisconnected: event => {
// Adjust user interface.
},
}
>
{/* ... */}
When your client temporarily disconnects from a session, the Subscriber objects in clients subscribing to a stream you publish dispatch events when your published stream drops and when (and if) it resumes automatically. For more information, see Automatic reconnection.
For sample code that demonstrates the use of these events, see the opentok-reconnection repo on GitHub.
When the connection is dropped and the client tries to reconnect, the Session object dispatches a reconnecting event. When the connection is restored, the Session object dispatches a reconnected event. If the client cannot restore the connection, the client disconnects from the session, and the Session object dispatches the sessionDisconnected event.
In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states:
session.on(
sessionReconnecting: function() {
// Display a user interface notification.
},
sessionReconnected: function() {
// Adjust user interface.
},
sessionDisconnected: function() {
// Adjust user interface.
}
);
When your client temporarily disconnects from a session, the Subscriber objects in clients subscribing to a stream you publish dispatch events when your published stream drops and when (and if) it resumes automatically.
For more information, see Automatic reconnection in the "Subscribing to streams" developer guide.
By default, any signals you send while your client is temporarily disconnected from a session are queued and sent when (and if) you successfully reconnect. You can set the retryAfterReconnect property to false in the options you pass into the Session.signal() method to prevent signals from being queued while you are disconnected.
For more information, see Preventing signals from being sent during automatic reconnection.
When the connection is dropped and the client tries to reconnect, the Session.ReconnectionListener.onReconnecting(Session session) method is called. When the connection is restored, the Session.ReconnectionListener.onReconnected(Session session) method is called.
If the client cannot restore the connection, the client disconnects from the Vonage Video session, and the Session.SessionListener.onDisconnected(Session session) method is called.
In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states:
// In the implementation of the Session.ReconnectionListener interface
@Override
public void onReconnecting(session) {
// Display a user interface notification.
}
public void onReconnected(session) {
// Adjust user interface.
}
// In the implementation of the Session.SessionListener interface
@Override
public void onDisconnected(session) {
// Adjust user interface.
}
When your client temporarily disconnects from a session, methods in the implementations of the SubscriberKit.StreamListener interface in clients subscribing to a stream you publish are called when your published stream drops and when (and if) it resumes automatically.
For more information, see the Detecting when streams leave a session and reconnection step in the Subscribe to streams tutorial.
By default, any signals you send while your client is temporarily disconnected from a session are queued and sent when (and if) you successfully reconnect. You can use the Session.sendSignal(String type, String data, Connection connection, boolean retryAfterReconnect) method and set the retryAfterReconnect parameter to false to prevent signals from being queued while you are disconnected.
For more information, see Preventing signals from being sent during automatic reconnection.
When the connection is dropped and the client tries to reconnect, the OTSessionDelegate sessionDidBeginReconnecting(_:) message is sent to the OTSession object's delegate. When the connection is restored, the OTSessionDelegate sessionDidReconnect(_:) message is sent. If the client cannot restore the connection, the OTSessionDelegate sessionDidDisconnect(_:) message is sent.
In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states.
// OTSession delegate callbacks:
func sessionDidBeginReconnecting(_ session:OTSession) {
// Display a user interface notification.
}
func sessionDidReconnect(_ session: OTSession) {
// Adjust user interface.
}
func sessionDidDisconnect(_ session: OTSession) {
// Adjust user interface.
}
When your client temporarily disconnects from a session, the OKSubscriberKitDelegate objects in clients subscribing to a stream you publish send messages when your published stream drops and when (and if) it resumes automatically.
For more information, see the Detecting when streams leave a session and reconnection step in the Subscribe to streams tutorial.
By default, any signals you send while your client is temporarily disconnected from a session are queued and sent when (and if) you successfully reconnect. You can use the OTSession signal(withType: string:connection:retryAfterReconnect:error:) method to prevent signals from being queued while you are disconnected.
For more information, see Signaling tutorial.
When the connection is dropped and the client tries to reconnect, the [OTSessionDelegate sessionDidBeginReconnecting:] message is sent to the OTSession object's delegate. When the connection is restored, the [OTSessionDelegate sessionDidReconnect:] message is sent. If the client cannot restore the connection, the [OTSessionDelegate sessionDidDisconnect:] message is sent.
In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states.
// OTSession delegate callbacks:
- (void)sessionDidBeginReconnecting:(OTSession*)session
{
// Display a user interface notification.
}
- (void)sessionDidReconnect:(OTSession*)session
{
// Adjust user interface.
}
- (void)sessionDidDisconnect:(OTSession*)session
{
// Adjust user interface.
}
When your client temporarily disconnects from a session, the OKSubscriberKitDelegate objects in clients subscribing to a stream you publish send messages when your published stream drops and when (and if) it resumes automatically. For more information, see the Detecting when streams leave a session and reconnection step in the Subscribe to streams tutorial.
By default, any signals you send while your client is temporarily disconnected from a session are queued and sent when (and if) you successfully reconnect. You can use the [OTSession signalWithType:string:connection:retryAfterReconnect:error:] method to prevent signals from being queued while you are disconnected. For more information, see Preventing signals from being sent during automatic reconnection.
When the connection is dropped and the client tries to reconnect, the Session.ReconnectionStart event is sent. When the connection is restored, the Session.ReconnectionSuccess event is sent. If the client cannot restore the connection, the client disconnects from the Vonage Video session, and the Session.Disconnected event is sent.
In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states:
session.ReconnectionStart = Session_ReconnectionStart;
session.ReconnectionSuccess = Session_ReconnectionSuccess;
session.Disconnected = Session_Disconnected;
private void Session_ReconnectionStart(object sender, EventArgs e)
{
// Display a user interface notification.
}
private void Session_ReconnectionSuccess(object sender, EventArgs e)
{
// Adjust user interface.
}
private void Session_Disconnected(object sender, EventArgs e)
{
// Adjust user interface.
}
When your client temporarily disconnects from a session, Subscriber objects in clients subscribing to the stream send Subscriber.StreamDisconnected and Subscriber.StreamDisconnected events when your published stream drops and when (and if) it resumes automatically. For more information, see the Detecting when streams leave a session and reconnection step in the Subscribe to streams tutorial.
By default, any signals you send while your client is temporarily disconnected from a session are queued and sent when (and if) you successfully reconnect. You can use the Session.SendSignal(type, signal, connection, retryAfterReconnect) method and set the retryAfterReconnect parameter to false to prevent signals from being queued while you are disconnected. For more information, see Preventing signals from being sent during automatic reconnection.
When the connection is dropped and the client tries to reconnect, the on_reconnection_started callback function of the otc_session_callbacks struct is called. When the connection is restored, the on_reconnected callback function of the otc_session_callbacks struct is called.
If the client cannot restore the connection, the client disconnects from the OpenTok session, and the on_disconnected callback function of the otc_session_callbacks struct is called.
In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states.
When another client temporarily disconnects from a session, the on_disconnected callback function of the otc_subscriber_callbacks struct for a subscriber to a stream published by that client is called. The on_reconnected callback function of the otc_subscriber_callbacks struct for the subscriber is invoked when (and if) the client reconnects and the stream resumes automatically.
For more information, see the Detecting when streams leave a session and reconnection step in the Subscribe to streams tutorial.
By default, any signals you send while your client is temporarily disconnected from a session are queued and sent when (and if) you successfully reconnect.
To prevent signals from being queued while you are disconnected. you can use the otc_session_send_signal_with_options() function or the otc_session_send_signal_to_connection_with_options () function and set the retryAfterReconnect member to false in the otc_signal_options you pass into the function.
For more information, see Preventing signals from being sent during automatic reconnection.
Troubleshooting session connection issues (JavaScript)
The Session.connect() method has a callback function which is passed an optional error parameter. If this parameter is present and defined (not null or undefined), then there was an error when connecting. Looking for this error in your code will help you decipher why the end-user was unable to connect:
session.connect(token, function(err) {
if (err) {
// handle error
} else {
// connection succeeded
}
});
A large number of errors that come back when attempting to connect are due to either invalid or expired tokens.
Another common reason for connecting to a session failing is due to the end user's internet connection. Examples of this include:
- The end user has lost their internet connection
- The end user has common ports blocked because they're on a restrictive network.
This will result in an error with the code 1006. We recommend you handle this using the code below. Other reasons for connecting to a session failing include the Vonage video servers being down, or that some kind of unexpected error happened (such as a 500-level error in the server). While this doesn't happen often, it is good practice to handle these errors.
If you follow these instructions your error handling code should look something like:
session.connect(token, function(err) {
if (err) {
if (err.name === "OT_NOT_CONNECTED") {
showMessage('Failed to connect. Please check your connection and try connecting again.');
} else {
showMessage('An unknown error occurred connecting. Please try again later.');
}
}
});
You can lose your connection after you have already successfully connected to a Session. You can handle this case by listening for the sessionDisconnected event with a reason of "networkDisconnected":
session.on({
sessionDisconnected: function(event) {
if (event.reason === 'networkDisconnected') {
showMessage('You lost your internet connection.'
+ 'Please check your connection and try connecting again.');
}
}
});
Troubleshooting session connection issues (React Native)
A large number of errors that come back when attempting to connect are due to either invalid or expired tokens. Make sure you are following the token best practices outlined here.
Another common reason for connecting to a session failing is due to the end user's internet connection. Examples of this include:
- The end user has lost their internet connection
- The end user has common ports blocked because they're on a restrictive network.