Skip to main content
Version: 3.x.x

Raise Hand using PubSub - Android

Let us see how we can use PubSub to implement Raise Hand functionality. If you are not familiar with the PubSub mechanism and pubSub class, you can follow this guide.

Implementing Raise Hand​

  1. Choose the topic every participant publishes and subscribes to for raised hands. This guide uses RAISE_HAND.
  2. On the raiseHand button, publish any message to that specific topic.
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)

// Sending the Message using the publish method
lifecycleScope.launch {
try {
room!!.pubSub.publish("RAISE_HAND", "Raise Hand", publishOptions)
} catch (e: VideoSDKException) {
// Room is not joined, is reconnecting, or the message could not be sent
Log.d("VideoSDK", "publish failed: ${e.code} ${e.message}")
}
}
}
}
  1. 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 registered only once the room is connected.
caution

publish() and subscribe() are suspend functions, so call them from a coroutine. subscribe() requires a joined room. Calling it before the room is joined — for example directly in onCreate() of the activity that joins the room — throws a VideoSDKException with code 3035 (ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) and fires onError with the same code. Always subscribe from onRoomJoined().

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 connected
lifecycleScope.launch {
try {
room!!.pubSub.subscribe("RAISE_HAND", pubSubMessageListener)
} catch (e: VideoSDKException) {
// Defensive: the subscription can still fail here
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}")
}
}
}
}
note

onRoomJoined() also fires after a reconnect that needed a fresh join, and after switchTo(). Your 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