Skip to main content
Version: 0.1.x

Notify Attendees in Realtime - Android

When doing livestream, you may want to broadcast message to all the viewers at once.

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

Notifying Attendees

  1. We will be publish the message that the sender typed in the EditText field to the topic NOTIFY_ATTENDEES.

class SpeakerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//...
findViewById(R.id.btnSend).setOnClickListener(view -> sendMessage());
}

private fun sendMessage() {
// get message from EditText
val message: String = etmessage.getText().toString()
if (!TextUtils.isEmpty(message)) {
val publishOptions = PubSubPublishOptions()
publishOptions.setPersist(true)

// Sending the Message using the publish method
meeting!!.pubSub.publish("NOTIFY_ATTENDEES", message, publishOptions)

// Clearing the message input
etmessage.setText("")
} else {
Toast.makeText(
this@SpeakerActivity, "Please Enter Message",
Toast.LENGTH_SHORT
).show()
}
}
}
  1. Now let us show an alert to all the viewers displaying the message posted by the speaker.
class ViewerActivity : AppCompatActivity() {

// PubSubMessageListener
var pubSubMessageListener =
PubSubMessageListener { message ->
if(meeting!!.localparticipant.mode == "VIEWER"){
Toast.makeText(
this@ViewerActivity, message.message,
Toast.LENGTH_SHORT
).show()
}
}

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

//...

// Subscribe for 'NOTIFY_ATTENDEES' topic
val pubSubMessageList = meeting!!.pubSub.subscribe("NOTIFY_ATTENDEES", pubSubMessageListener)

for (message in pubSubMessageList) {
// Persisted messages
if(meeting!!.localparticipant.mode == "VIEWER"){
Toast.makeText(
this@ViewerActivity, message.message,
Toast.LENGTH_SHORT
).show()
}
}
}
}

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