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
- The first step is choosing the topic that all participants publish and subscribe to, so everyone knows when a participant raises their hand. We will be using
RAISE_HANDas the topic for this one. - On the raiseHand button, publish any message to that specific topic.
- Swift
extension RoomViewController {
// Button that publishes the raise hand message when tapped
@IBAction func raiseHandTapped(_ sender: Any) {
Task {
do {
try await room?.pubSub.publish(topic: "RAISE_HAND", message: "Raise Hand")
} catch {
print("publish error: \(error)")
}
}
}
}
- Now let us subscribe to the
RAISE_HANDtopic after joining, and show an alert to all the participants showing who raised the hand.
- Swift
extension RoomViewController: RoomEventListener {
/// Room joined
func onRoomJoined() {
//...
Task {
do {
try await room?.pubSub.subscribe(topic: "RAISE_HAND", listener: self)
} catch {
print("subscribe error: \(error)")
}
}
}
}
extension RoomViewController: PubSubMessageListener {
func onMessageReceived(_ message: PubSubMessage) {
print("Message Received:= " + message.message)
let localParticipantID = room?.localParticipant.id
if(message.topic == "RAISE_HAND"){
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 - 125, y: self.view.frame.size.height - 100, 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

