Suggestions

close search

Signaling — Android

Use the OpenTok signaling API to send text and data between clients connected to an OpenTok session.

For conceptual information on the OpenTok signaling API, see the Signaling overview developer guide.

This topic includes the following sections:

Sending a signal to a specific client in a session

To send a signal to a specific client in a session, call the sendSignal(String type, String data, Connection connection) method of the Session object:

mSession.sendSignal("chat", "Hello", mConnection1);

The type parameter is a string value that clients can filter on when listening for signals. Set this to an empty string if you do not need to set a type.

The data parameter is the data payload (a String) you send with the message. The limit to the size of data is 8KB.

The connection parameter is a Connection object, corresponding to client connected to the session that you want to signal. You obtain references to Connection objects, corresponding to clients connected to the session, in the onConnectionCreated(Session session, Connection connection) method of the Session.ConnectionListener object implemented for the Session.

Sending a signal to all clients in a session

To send a signal to all clients in a session, call the sendSignal(String type, String data) method of the Session object:

mSession.sendSignal("chat", "Hello");

The type parameter is a string value that clients can filter on when listening for signals. Set this to an empty string if you do not need to set a type.

The data parameter is the data payload (a String) you send with the message. The limit to the size of data is 8KB.

Receiving signals in a session

To start receiving all signals, call the setSignalListener(Session.SignalListener listener) method of the Session object, passing in an object that implements the Session.SignalListener interface:

 mSession.setSignalListener(this);

The onSignalReceived(Session session, String type, String data, Connection connection) method of the SignalListener object is called when a signal is received in the session:

@Override
protected void onSignalReceived(Session session, String type, String data, Connection connection) {
    String myConnectionId = session.getConnection().getConnectionId();
    if (connection != null && connection.getConnectionId().equals(myConnectionId)) {
        // Signal received from another client
    }
}

Preventing signals from being sent during automatic reconnection

Clients will attempt to automatically reconnect to a session they disconnect unexpectedly (for example, due to a drop in network connectivity). By default, any signals you send while the client is temporarily disconnected from a session are queued and sent when (and if) it successfully reconnects. 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 the client is disconnected. For more information, see Automatic reconnection.