Skip to main content
Version: 2.0.x

Notify Attendees in Realtime - iOS

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 TextField field to the topic NOTIFY_ATTENDEES.
extension MeetingViewController: MeetingEventListener {
func onMeetingJoined() {
//...
Task {
await meeting?.pubsub.subscribe(topic: "NOTIFY_ATTENDEES", forListener: self)
}
//...
}
}

extension MeetingViewController: PubSubMessageListener {
func onMessageReceived(_ message: PubSubMessage) {
//...
if (message.topic == "NOTIFY_ATTENDEES") {
//...
print("Message Received:= " + message.message)
}
}
}
  1. Now let us show an alert to all the viewers displaying the message posted by the speaker.
extension MeetingViewController: MeetingEventListener {
/// Meeting started
func onMeetingJoined() {
//...
Task {
await meeting?.pubsub.subscribe(topic: "NOTIFY_ATTENDEES", forListener: self)
}
}
}

extension MeetingViewController: PubSubMessageListener {
func onMessageReceived(_ message: PubSubMessage) {
let localParticipantID = participants.first(where: { $0.isLocal == true })?.id
if(message.topic == "NOTIFY_ATTENDEES"){
self.showToast(message: "Message: \(message.message)", font: .systemFont(ofSize: 18))
}
}
}

extension UIViewController {
func showToast(message: String, font: UIFont) {
let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2, y: self.view.frame.size.height, width: 250, height: 50))
toastLabel.font = font
toastLabel.textAlignment = .center;
toastLabel.text = message
self.view.addSubview(toastLabel)
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
toastLabel.removeFromSuperview()
}
}
}

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