-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spec complete for Presence in line with [1].
[1] - ably/specification#200
- Loading branch information
1 parent
609ea6d
commit a6de834
Showing
15 changed files
with
594 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Ably | ||
|
||
internal final class DefaultOccupancy: Occupancy, EmitsDiscontinuities { | ||
private let chatAPI: ChatAPI | ||
private let roomID: String | ||
private let logger: InternalLogger | ||
public nonisolated let featureChannel: FeatureChannel | ||
|
||
internal nonisolated var channel: any RealtimeChannelProtocol { | ||
featureChannel.channel | ||
} | ||
|
||
internal init(featureChannel: FeatureChannel, chatAPI: ChatAPI, roomID: String, logger: InternalLogger) { | ||
self.featureChannel = featureChannel | ||
self.chatAPI = chatAPI | ||
self.roomID = roomID | ||
self.logger = logger | ||
} | ||
|
||
// (CHA-04a) Users may register a listener that receives occupancy events in realtime. | ||
// (CHA-04c) When a regular occupancy event is received on the channel (a standard PubSub occupancy event per the docs), the SDK will convert it into occupancy event format and broadcast it to subscribers. | ||
internal func subscribe(bufferingPolicy: BufferingPolicy) async -> Subscription<OccupancyEvent> { | ||
logger.log(message: "Subscribing to occupancy events", level: .debug) | ||
let subscription = Subscription<OccupancyEvent>(bufferingPolicy: bufferingPolicy) | ||
channel.subscribe("[meta]occupancy") { [logger] message in | ||
logger.log(message: "Received occupancy message: \(message)", level: .debug) | ||
let data = message.data as? [String: Any] ?? [:] | ||
let metrics = data["metrics"] as? [String: Any] ?? [:] | ||
|
||
let data1 = metrics["connections"] as? Int ?? 0 | ||
let data2 = metrics["presenceMembers"] as? Int ?? 0 | ||
|
||
let occupancyEvent = OccupancyEvent(connections: data1, presenceMembers: data2) | ||
logger.log(message: "Emitting occupancy event: \(occupancyEvent)", level: .debug) | ||
subscription.emit(occupancyEvent) | ||
} | ||
return subscription | ||
} | ||
|
||
// (CHA-O3) Users can request an instantaneous occupancy check via the REST API. The request is detailed here (https://sdk.ably.com/builds/ably/specification/main/chat-features/#rest-occupancy-request), with the response format being a simple occupancy event | ||
internal func get() async throws -> OccupancyEvent { | ||
logger.log(message: "Getting occupancy for room: \(roomID)", level: .debug) | ||
return try await chatAPI.getOccupancy(roomId: roomID) | ||
} | ||
|
||
// (CHA-O5) Users may subscribe to discontinuity events to know when there’s been a break in occupancy. Their listener will be called when a discontinuity event is triggered from the room lifecycle. For occupancy, there shouldn’t need to be user action as most channels will send occupancy updates regularly as clients churn. | ||
internal func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> { | ||
await featureChannel.subscribeToDiscontinuities() | ||
} | ||
} |
Oops, something went wrong.