Skip to main content
Version: 2.0.x

Raise Hand using PubSub - iOS

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

extension MeetingViewController: PubSubMessageListener {
func onMessageReceived(_ message: PubSubMessage) {
//...
if (message.topic == "RAISE_HAND") {
//...
print("Message Received:= " + message.message)
}
}
}
  1. Now let us show an alert to all the participants showing who raised the hand.
extension MeetingViewController: MeetingEventListener {
/// Meeting started
func onMeetingJoined() {
//...
Task {
await meeting?.pubsub.subscribe(topic: "RAISE_HAND", forListener: self)
}
}
}

extension MeetingViewController: PubSubMessageListener {
func onMessageReceived(_ message: PubSubMessage) {
print("Message Received:= " + message.message)
let localParticipantID = participants.first(where: { $0.isLocal == true })?.id
if(message.topic == RAISE_HAND_TOPIC){
self.showToast(message: "\(message.senderId == localParticipantID ? "You" : "\(message.senderName)") raised hand 🖐🏼", 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