Skip to content

DataChannel

Lejla Solak edited this page Feb 10, 2025 · 2 revisions



send(text, sendTextListener)

Description

Sends data through the data channel to all other call participants who also have data channel enabled.

Arguments

  • text: String - Data to be sent through the data channel.
  • sendTextListener: SendTextListener - Listener to handle success and failure of sending data.

Returns

  • N/A

Example

private void example() {
    InfobipRTC infobipRTC = InfobipRTC.getInstance();
    ApplicationCall applicationCall = infobipRTC.getActiveApplicationCall();
    DataChannel dataChannel = applicationCall.dataChannel();
    dataChannel.send("Hello World!", new SendTextListener() {
        @Override
        public void onSendSuccess(String id) {
            Log.d("WebRTC", String.format("Successfully sent text with id: %s", id));
        }

        @Override
        public void onSendFailure(ErrorCode errorCode) {
            Log.e("WebRTC", String.format("Error occurred when sending text: %s.", errorCode.getDescription()));
        }
    });
}



send(text, to, sendTextListener)

Description

Sends data through the data channel directly to a chosen endpoint participating in the call. This method ensures that the text is only sent to the chosen recipient and therefore is not received by any other call participants.

Arguments

  • text: String - Data to be sent through the data channel.
  • to: Endpoint - Endpoint to send data directly to.
  • sendTextListener: SendTextListener - Listener to handle success and failure of sending data.

Returns

  • N/A

Example

private void example() {
    InfobipRTC infobipRTC = InfobipRTC.getInstance();
    WebrtcCall webrtcCall = (WebrtcCall) infobipRTC.getActiveCall();
    DataChannel dataChannel = webrtcCall.dataChannel();
    dataChannel.send("Hello World!", webrtcCall.counterpart(), new SendTextListener() {
        @Override
        public void onSendSuccess(String id) {
            Log.d("WebRTC", String.format("Successfully sent text with id: %s", id));
        }

        @Override
        public void onSendFailure(ErrorCode errorCode) {
            Log.e("WebRTC", String.format("Error occurred when sending text: %s.", errorCode.getDescription()));
        }
    });
}



setEventListener(dataChannelEventListener)

Description

Configures event handler for data channel events.

Arguments

  • dataChannelEventListener: DataChannelEventListener - An interface that should be implemented in order to properly handle data channel events.

Returns

  • N/A

Example

private void example() {
    InfobipRTC infobipRTC = InfobipRTC.getInstance();
    ApplicationCall applicationCall = infobipRTC.getActiveApplicationCall();
    DataChannel dataChannel = applicationCall.dataChannel();
    dataChannel.setEventListener(new DataChannelEventListener() {
        @Override
        public void onTextDelivered(TextDeliveredEvent textDeliveredEvent) {
            if (textDeliveredEvent.isDelivered()) {
                Toast.makeText(getApplicationContext(), String.format("Successfully delivered text with id: %s", textDeliveredEvent.getId()), Toast.LENGTH_LONG);
            } else {
                Toast.makeText(getApplicationContext(), String.format("Failed to deliver text with id: %s", textDeliveredEvent.getId()), Toast.LENGTH_LONG);
            }
        }

        @Override
        public void onTextReceived(TextReceivedEvent textReceivedEvent) {
            if (textReceivedEvent.isDirect()) {
                Toast.makeText(getApplicationContext(), String.format("Received direct text from %s: %s", textReceivedEvent.getFrom().identifier(), textReceivedEvent.getText()), Toast.LENGTH_LONG);
            } else {
                Toast.makeText(getApplicationContext(), String.format("Received text from %s: %s", textReceivedEvent.getFrom().identifier(), textReceivedEvent.getText()), Toast.LENGTH_LONG);
            }
        }

        @Override
        public void onBroadcastTextReceived(BroadcastTextReceivedEvent broadcastTextReceivedEvent) {
            Toast.makeText(getApplicationContext(), String.format("Received broadcast text: %s", broadcastTextReceivedEvent.getText()), Toast.LENGTH_LONG);
        }
    });
}



getEventListener()

Description

Returns event handler for data channel events.

Arguments

  • none

Returns

Example

InfobipRTC infobipRTC = InfobipRTC.getInstance();
ApplicationCall applicationCall = infobipRTC.getActiveApplicationCall();
DataChannel dataChannel = applicationCall.dataChannel();
DataChannelEventListener dataChannelEventListener = dataChannel.getEventListener();

Tutorials

Migration guides

Reference documentation

Clone this wiki locally