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_HAND
as 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(R.id.btnRaiseHand).setOnClickListener(view -> sendRaiseHandMessage());
}
private fun sendRaiseHandMessage() {
val publishOptions = PubSubPublishOptions()
publishOptions.setPersist(false)
// Sending the Message using the publish method
meeting!!.pubSub.publish("RAISE_HAND", "Raise Hand", publishOptions)
}
}
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
meeting.pubSub.publish("RAISE_HAND", "Raise Hand", publishOptions);
}
}
- Now let us show an alert to all the participants showing who raised the hand.
- Kotlin
- Java
class MainActivity : AppCompatActivity() {
// PubSubMessageListener
var pubSubMessageListener =
PubSubMessageListener { message ->
// Participant raise hand
Toast.makeText(
this@MainActivity, message.senderName + " raise hand",
Toast.LENGTH_SHORT
).show()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
//...
// Subscribe for 'RAISE_HAND' topic
val pubSubMessageList = meeting!!.pubSub.subscribe("RAISE_HAND", pubSubMessageListener)
}
}
public class MainActivity extends AppCompatActivity {
// PubSubMessageListener
private PubSubMessageListener pubSubMessageListener = new PubSubMessageListener() {
@Override
public void onMessageReceived(PubSubMessage message) {
// Participant raise hand
Toast.makeText(
MainActivity.this, message.senderName + " raise hand",
Toast.LENGTH_SHORT
).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//..
// Subscribe for 'RAISE_HAND' topic
List<PubSubMessage> pubSubMessageList = meeting.pubSub.subscribe("RAISE_HAND", pubSubMessageListener);
}
}
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