You can do it using DataTrack API provided by the Twilio. This allows you to send messages between Participants connected to a Room.
Create the instance for the LocalDataTrack
private var mLocalDataTrack = LocalDataTrack.create(mActivity)
Create the room and set mLocalDataTrack
val connectOptionsBuilder = Builder(accessToken)
.dataTracks(Collections.singletonList(mLocalDataTrack))
.roomName(roomId.toString())
Implement the RemoteParticipant.Listener
in your view. Listener contains onDataTrackSubscribed
method which you need to override and set listener
override fun onDataTrackSubscribed(
remoteParticipant: RemoteParticipant,
remoteDataTrackPublication: RemoteDataTrackPublication,
remoteDataTrack: RemoteDataTrack
) {
Timber.e("onDataTrackSubscribed")
remoteDataTrack.setListener(this)
}
then implement RemoteDataTrack.Listener
interface in your activity.
override fun onMessage(remoteDataTrack: RemoteDataTrack, messageBuffer: ByteBuffer) {
Timber.e("Message is %s", messageBuffer)
}
override fun onMessage(remoteDataTrack: RemoteDataTrack, message: String) {
if (message == "disconnect") {
mRoom?.disconnect()
}
}
And send a message when the host disconnect from the room.
mLocalDataTrack?.send("disconnect")
When the host leaves from the room by pressing button the message will send to the rest of the participants of the room and disconnected.