Ringing

The Call object provides several options to ring and notify users about a call.

Ringing

If a call was not created before, you should first create it and pass the ring value to true. For this, you can use the create method from the Call object.

let call = streamVideo.call(callType: "default", callId: callId)
let callResponse = try await call.create(members: members, ring: true)

When ring is true, a VoIP notification will be sent to the members, provided you have the required setup for CallKit and PushKit. If you don’t have a VoIP push setup, a regular push notification will be sent to the members. For more details around push notifications, please check this page.

If ring is false, no push notification will be sent.

If a call was created before, the method will just get it and send the notifications. If you are sure that a call exists, you can use the get method instead:

let call = streamVideo.call(callType: "default", callId: callId)
let callResponse = try await call.get(ring: true)

Additionally, you can use the ring method which is just a shortcut for the method above:

let call = streamVideo.call(callType: "default", callId: callId)
let callResponse = try await call.ring()

Notifying users

In some cases, you just want to notify users that you joined a call, instead of ringing. To do this, you should use the notify option:

let call = streamVideo.call(callType: "default", callId: callId)
let callResponse = try await call.create(members: members, notify: true)

When notify is true, a regular push notification will be sent to all the members. This can be useful for livestreams apps or huddles.

Similarly to ringing, you can use the get method if you are sure that the call exists:

let call = streamVideo.call(callType: "default", callId: callId)
let callResponse = try await call.get(notify: true)

You can also use a shortcut of this method, with the notify method:

let call = streamVideo.call(callType: "default", callId: callId)
let callResponse = try await call.notify()

Listening to Ringing Events

When the app is active, a web socket event called CallRingEvent will be sent if someone rings the currently logged in user. You can listen to this event to show an in-app calling screen.

Task {
    let call = streamVideo.call(callType: "default", callId: "123")
    for await event in call.subscribe(for: CallRingEvent.self) {
        print(event)
    }
}

The CallRingEvent has a video boolean property, which provides info whether the call has video (or is audio only). You can use this information to customize the in-app calling screen.

Additionally, you can access the current ringingCall from the StreamVideo object’s state:

let ringingCall = streamVideo.state.ringingCall

UI Components

If you use our CallViewModel and the default UI components, the CallRingEvent is already handled and an incoming calling screen is shown automatically.

You can find more details about it here.

Ringing Sounds

The UI components come with sounds for both the incoming and outgoing screens. You can change these sounds with your custom ones, by changing the corresponding values in the Sounds class:

let sounds = Sounds()
sounds.incomingCallSound = "your_custom_sound"
let appearance = Appearance(sounds: sounds)
let streamVideoUI = StreamVideoUI(streamVideo: streamVideo, appearance: appearance)

AutoLeave Policy

There may be scenarios where once a call is concluded there is no point for a user to remain in the call if the other participants have already left. In cases like that you may find youself in a position where you need to last participant to automatically leave the call. Luckily, the StreamVideoSwiftUI SDK makes that very easy by allowing you to set the ParticipantAutoLeavePolicy, like below:

let callViewModel = CallViewModel()
callViewModel.participantAutoLeavePolicy = LastParticipantAutoLeavePolicy()

By doing that, we are instructing the CallViewModel to observe the participants during the ringing flow call (incoming or outgoing). Once a user remain the last one, the callViewModel will automatically trigger the flow to leave the call.

The participantAutoLeavePolicy is set to DefaultParticipantAutoLeavePolicy which is a no-operation policy, meaning that if the user remain in the call alone, no automatic action will be performed.

© Getstream.io, Inc. All Rights Reserved.