Skip to main content
Version: 4.x.x

Display Attendees Count - iOS

In this guide, we will see how you can display the number of attendees in realtime.

note

Before going forward in this guide, do make sure all the attendees join the live stream with mode as SIGNALLING_ONLY

  • We will get all the participants except the local participant using the participants property of the Room class. From these, we will count the participants who have joined as SIGNALLING_ONLY and display that count. The local participant is counted too when its mode is SIGNALLING_ONLY.
  • Count again whenever a participant joins, leaves or changes mode, so the number stays correct.
class LiveStreamViewController: ObservableObject {
@Published var viewerParticipantCount: Int = 0

//...

func updateViewerParticipantCount() {
guard let room = room else { return }
// remote participants who joined as SIGNALLING_ONLY
let remoteViewers = room.participants.values.filter { $0.mode == .SIGNALLING_ONLY }.count
// count yourself too if you joined as SIGNALLING_ONLY
let localViewer = room.localParticipant.mode == .SIGNALLING_ONLY ? 1 : 0
viewerParticipantCount = remoteViewers + localViewer
}
}

extension LiveStreamViewController: RoomEventListener {
func onRoomJoined() {
//...
updateViewerParticipantCount()
}

func onParticipantJoined(_ participant: Participant) {
//...
updateViewerParticipantCount()
}

func onParticipantLeft(_ participant: Participant, reason: LeaveReason) {
//...
updateViewerParticipantCount()
}

func onParticipantModeChanged(participantId: String, mode: Mode) {
//...
updateViewerParticipantCount()
}
}

API Reference​

The API references for all the methods utilized in this guide are provided below.

Got a Question? Ask us on discord


Was this helpful?

Not helpfulHelpful