Raise Hand using PubSub - Android
Let's see how to use PubSub to implement Raise Hand. If you are not familiar with the PubSub mechanism and pubSub class, you can follow this guide.
Implementing Raise Hand
- Choose the topic every participant publishes and subscribes to for raised hands. This guide uses
RAISE_HAND. - On the raiseHand button, publish any message to that specific topic.
publish()is asuspendfunction, so call it from a coroutine. It throws aVideoSDKExceptionwhen the room is not joined, isRECONNECTING, or the message could not be sent.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//...
findViewById<View>(R.id.btnRaiseHand).setOnClickListener { sendRaiseHandMessage() }
}
private fun sendRaiseHandMessage() {
val publishOptions = PubSubPublishOptions(persist = false)
lifecycleScope.launch {
try {
// Sending the Message using the publish method
room!!.pubSub.publish("RAISE_HAND", "Raise Hand", publishOptions)
} catch (e: VideoSDKException) {
// Room is not joined yet, is reconnecting, or the message could not be sent
Log.d("VideoSDK", "publish failed: ${e.code} ${e.message}")
}
}
}
}
- Now let us show an alert to all the participants showing who raised the hand. Subscribe to the topic from
onRoomJoined(), so the subscription is made once the room is joined.subscribe()is asuspendfunction, so launch a coroutine for it.
subscribe() requires a joined room. Called before the join completes, for example directly in onCreate() of the activity that joins the room, it throws a VideoSDKException with code 3035 (ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED).
class MainActivity : AppCompatActivity() {
// PubSubMessageListener
val pubSubMessageListener = object : PubSubMessageListener {
override fun onMessageReceived(message: PubSubMessage) {
Toast.makeText(
this@MainActivity, "${message.senderName} just raised the hand",
Toast.LENGTH_SHORT
).show()
}
}
private val roomEventListener = object : RoomEventListener() {
override fun onRoomJoined(switchRoomId: String?) {
// Subscribe for 'RAISE_HAND' topic once the room is joined
lifecycleScope.launch {
try {
room!!.pubSub.subscribe("RAISE_HAND", pubSubMessageListener)
} catch (e: VideoSDKException) {
Log.d("VideoSDK", "subscribe failed: ${e.code} ${e.message}")
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//...
room!!.addEventListener(roomEventListener)
lifecycleScope.launch {
try {
room!!.join()
} catch (e: VideoSDKException) {
Log.d("VideoSDK", "join failed: ${e.code} ${e.message}")
}
}
}
}
onRoomJoined() also fires after a reconnect that needed a fresh join, and after switchTo(). The subscription is kept in both cases, so subscribing again with the same listener throws 3073 (ERROR_INVALID_PARAMETER). The catch above handles it.
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord

