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
- 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_HANDas the topic for this one. - On the raiseHand button, publish any message to that specific topic.
- Kotlin
- Java
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}")
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//...
findViewById(R.id.btnRaiseHand).setOnClickListener(view -> sendRaiseHandMessage());
}
private void sendRaiseHandMessage()
{
PubSubPublishOptions publishOptions = new PubSubPublishOptions();
publishOptions.setPersist(false);
// Sending the Message using the publish method
try {
meeting.pubSub.publish("RAISE_HAND", "Raise Hand", publishOptions);
} catch (Exception e) {
// Meeting is not connected yet, or is reconnecting
Log.d("VideoSDK", "publish failed: " + e.getMessage());
}
}
}
- 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().
- Kotlin
- Java
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()
}
}
public class MainActivity extends AppCompatActivity {
// PubSubMessageListener
PubSubMessageListener pubSubMessageListener = new PubSubMessageListener() {
@Override
public void onMessageReceived(PubSubMessage message) {
Toast.makeText(MainActivity.this,
message.getSenderName() + " just raised the hand",
Toast.LENGTH_SHORT).show();
}
};
private final MeetingEventListener meetingEventListener = new MeetingEventListener() {
@Override
public void onMeetingJoined() {
// Subscribe for 'RAISE_HAND' topic once the meeting is connected
try {
meeting.pubSub.subscribe("RAISE_HAND", pubSubMessageListener);
} catch (Exception e) {
// Defensive: the PubSub connection can still be unavailable here
Log.d("VideoSDK", "subscribe failed: " + e.getMessage());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
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

