Skip to main content
Version: 2.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. First step in raise hand is choosing the topic which all the participants will publish and subscribe to know some participant raise their hand. We will be using RAISE_HAND as the topic for this one.
  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()
publishOptions.isPersist = false

// Sending the Message using the publish method
try {
meeting!!.pubSub.publish("RAISE_HAND", "Raise Hand", publishOptions)
} catch (e: Exception) {
// Meeting is not connected yet, or is reconnecting
Log.d("VideoSDK", "publish failed: ${e.message}")
}
}
}
  1. Now let us show an alert to all the participants showing who raised the hand. Subscribe to the topic from onMeetingJoined(), so the subscription is registered only once the meeting is connected.
caution

subscribe() requires a joined meeting. Calling it before the meeting reaches the CONNECTED state — for example directly in onCreate() of the activity that joins the meeting — throws an IllegalStateException and fires onError with code 3001. Always subscribe from onMeetingJoined().

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 meetingEventListener = object : MeetingEventListener() {
override fun onMeetingJoined() {
// Subscribe for 'RAISE_HAND' topic once the meeting is connected
try {
meeting!!.pubSub.subscribe("RAISE_HAND", pubSubMessageListener)
} catch (e: Exception) {
// Defensive: the PubSub connection can still be unavailable here
Log.d("VideoSDK", "subscribe failed: ${e.message}")
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//...

meeting!!.addEventListener(meetingEventListener)
meeting!!.join()
}
}

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