-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRealtime.swift
More file actions
654 lines (579 loc) · 23.1 KB
/
Copy pathRealtime.swift
File metadata and controls
654 lines (579 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import Foundation
import AsyncHTTPClient
import NIO
import NIOHTTP1
open class Realtime : Service {
// Diagnostic messages go to stderr so they don't interleave with any stdout
// the host process may be asserting on (e.g. SDK integration tests).
private static func logDiagnostic(_ message: String) {
FileHandle.standardError.write(Data("\(message)\n".utf8))
}
private let TYPE_ERROR = "error"
private let TYPE_EVENT = "event"
private let TYPE_PONG = "pong"
private let DEBOUNCE_NANOS = 1_000_000
private let HEARTBEAT_INTERVAL: UInt64 = 20_000_000_000 // 20 seconds in nanoseconds
private var socketClient: WebSocketClient? = nil
private var activeSubscriptions = [String: RealtimeCallback]()
private var pendingSubscribes = [String: [String: Any]]()
private var pendingPresence: [String: Any]? = nil
private var appConnected = false
private var heartbeatTask: Task<Void, Swift.Error>? = nil
/// Single-flight lock for `createSocket()`. While set, concurrent callers
/// join this task instead of issuing a second `WebSocketClient.connect()`.
/// Cleared once the underlying connect resolves or throws.
private var socketCreationTask: Task<Void, Swift.Error>? = nil
let connectSync = DispatchQueue(label: "ConnectSync")
let presenceSync = DispatchQueue(label: "PresenceSync")
/// Guards mutations and reads of `activeSubscriptions` and
/// `pendingSubscribes`. These are touched from both async caller contexts
/// (subscribe / unsubscribe / close closures) and the WebSocket delegate
/// thread (handleResponseConnected / handleResponseEvent), so every access
/// goes through this queue. Never hold this across an `await` — keep
/// critical sections short.
let subscriptionsSync = DispatchQueue(label: "SubscriptionsSync")
private var subCallDepth = 0
private var reconnectAttempts = 0
private var reconnect = true
private var onErrorCallbacks: [((Swift.Error?, HTTPResponseStatus?) -> Void)] = []
private var onCloseCallbacks: [(() -> Void)] = []
private var onOpenCallbacks: [(() -> Void)] = []
public func onError(_ callback: @escaping (Swift.Error?, HTTPResponseStatus?) -> Void) {
self.onErrorCallbacks.append(callback)
}
public func onClose(_ callback: @escaping () -> Void) {
self.onCloseCallbacks.append(callback)
}
public func onOpen(_ callback: @escaping () -> Void) {
self.onOpenCallbacks.append(callback)
}
private func startHeartbeat() {
stopHeartbeat()
heartbeatTask = Task {
do {
while !Task.isCancelled {
if let client = socketClient, client.isConnected {
client.send(text: #"{"type": "ping"}"#)
}
try await Task.sleep(nanoseconds: HEARTBEAT_INTERVAL)
}
} catch {
if !Task.isCancelled {
Realtime.logDiagnostic("Heartbeat task failed: \(error.localizedDescription)")
}
}
}
}
private func stopHeartbeat() {
heartbeatTask?.cancel()
heartbeatTask = nil
}
/// Idempotent socket opener. Both `subscribe()` and `upsertPresence()` can
/// call this; the single-flight lock (`socketCreationTask`) guarantees
/// only one `WebSocketClient.connect()` is ever in flight, so concurrent
/// callers join the same connection attempt instead of opening duplicates.
private func createSocket() async throws {
// Fast path: a usable socket is already there.
if let ws = socketClient, ws.isConnected {
return
}
// Atomically check / claim the in-flight slot via the existing serial
// queue. The actual connect work runs outside the queue's critical
// section.
let task: Task<Void, Swift.Error> = connectSync.sync {
if let existing = socketCreationTask {
return existing
}
let newTask = Task<Void, Swift.Error> { [weak self] in
guard let self = self else { return }
defer {
self.connectSync.sync { self.socketCreationTask = nil }
}
try await self.createSocketLocked()
}
socketCreationTask = newTask
return newTask
}
try await task.value
}
private func createSocketLocked() async throws {
let hasPendingPresence: Bool = presenceSync.sync { pendingPresence != nil }
let hasActiveSubscriptions: Bool = subscriptionsSync.sync { !activeSubscriptions.isEmpty }
guard hasActiveSubscriptions || hasPendingPresence else {
reconnect = false
try await closeSocket()
return
}
let queryParams = "project=\(client.config["project"]!)"
let url = "\(client.endPointRealtime!)/realtime?\(queryParams)"
if (socketClient != nil) {
reconnect = false
try await closeSocket()
}
// Cooperative cancellation point: disconnect() may have cancelled us
// while we awaited closeSocket(). Bail before allocating a new client.
if Task.isCancelled || isTornDown() {
return
}
var headers = HTTPHeaders()
if let jwt = client.config["jwt"], !jwt.isEmpty {
headers.add(name: "x-appwrite-jwt", value: jwt)
}
socketClient = WebSocketClient(
url,
tlsEnabled: !client.selfSigned,
headers: headers,
delegate: self
)
try await socketClient?.connect()
// disconnect() may also have run between WebSocketClient init and the
// moment connect() resolved. The handshake succeeded, but state has
// been torn down — close the freshly opened socket so it doesn't leak.
if Task.isCancelled || isTornDown() {
try? await closeSocket()
}
}
private func isTornDown() -> Bool {
let hasPendingPresence: Bool = presenceSync.sync { pendingPresence != nil }
let hasActiveSubscriptions: Bool = subscriptionsSync.sync { !activeSubscriptions.isEmpty }
return !hasActiveSubscriptions && !hasPendingPresence
}
private func sendUnsubscribeMessage(_ subscriptionIds: [String]) {
let ids = subscriptionIds.filter { !$0.isEmpty }
guard !ids.isEmpty, let ws = socketClient, ws.isConnected else {
return
}
let payload: [String: Any] = [
"type": "unsubscribe",
"data": ids.map { ["subscriptionId": $0] }
]
if let data = try? JSONSerialization.data(withJSONObject: payload),
let text = String(data: data, encoding: .utf8) {
ws.send(text: text)
}
}
/// Must be called with `subscriptionsSync` held.
private func generateUniqueSubscriptionIdLocked() throws -> String {
let attempts = activeSubscriptions.count + 1
for _ in 0..<attempts {
let id = ID.unique()
if activeSubscriptions[id] == nil {
return id
}
}
throw AppwriteError(message: "Failed to generate unique subscription id")
}
/// Must be called with `subscriptionsSync` held.
private func enqueuePendingSubscribeLocked(subscriptionId: String) {
guard let subscription = activeSubscriptions[subscriptionId] else {
return
}
pendingSubscribes[subscriptionId] = [
"subscriptionId": subscriptionId,
"channels": Array(subscription.channels),
"queries": subscription.queries
]
}
/// Close the WebSocket connection and drop all active subscriptions client-side.
/// Use this instead of calling `unsubscribe()` on every subscription when you want
/// to tear everything down.
public func disconnect() async throws {
subscriptionsSync.sync {
activeSubscriptions.removeAll()
pendingSubscribes.removeAll()
}
presenceSync.sync {
pendingPresence = nil
}
connectSync.sync { appConnected = false }
reconnect = false
// Capture and cancel any in-flight socket-creation task. Just nulling
// the reference is not enough — Swift tasks are cooperative, so the
// already-running body would otherwise proceed past its guard checks
// and assign `socketClient` after teardown, leaving a stray realtime
// connection alive with no subscriptions or presence.
let pendingTask: Task<Void, Swift.Error>? = connectSync.sync {
let t = socketCreationTask
socketCreationTask = nil
return t
}
pendingTask?.cancel()
try await closeSocket()
}
private func sendPendingSubscribes() {
guard let ws = socketClient, ws.isConnected else {
return
}
// The WebSocketClient becomes "connected" as soon as the WS upgrade
// succeeds — but the server only accepts `subscribe` frames after
// emitting its application-level `connected` event (which flips
// `appConnected` in handleResponseConnected). Sending early triggers
// a policy-violation close on real Appwrite, which reconnects and
// re-sends, looping forever. handleResponseConnected re-enqueues
// every active subscription and re-calls this method, so the queued
// rows are guaranteed to be sent once it's safe.
let isAppConnected: Bool = connectSync.sync { appConnected }
guard isAppConnected else {
return
}
let rows: [[String: Any]] = subscriptionsSync.sync {
if pendingSubscribes.isEmpty {
return []
}
let snapshot = Array(pendingSubscribes.values)
pendingSubscribes.removeAll()
return snapshot
}
if rows.isEmpty {
return
}
let payload: [String: Any] = [
"type": "subscribe",
"data": rows
]
if let data = try? JSONSerialization.data(withJSONObject: payload),
let text = String(data: data, encoding: .utf8) {
ws.send(text: text)
}
}
private func closeSocket() async throws {
stopHeartbeat()
guard let client = socketClient,
let group = client.threadGroup else {
return
}
if (client.isConnected) {
let promise = group.any().makePromise(of: Void.self)
client.close(promise: promise)
try await promise.futureResult.get()
}
try await group.shutdownGracefully()
}
private func getTimeout() -> Int {
switch reconnectAttempts {
case 0..<5: return 1000
case 5..<15: return 5000
case 15..<100: return 10000
default: return 60000
}
}
/**
* Convert channel value to string
* All Channel instances and String conform to ChannelValue
*/
private func channelToString(_ channel: ChannelValue) -> String {
return channel.toString()
}
/// Fire-and-forget presence upsert. Records the latest payload in state so
/// that — if the WebSocket isn't open yet, or later reconnects — the most
/// recent presence is automatically (re)sent on the next `connected` event.
/// When the socket is already open, the frame is sent immediately.
///
/// - Parameters:
/// - status: The presence status (required).
/// - presenceId: The presence ID (required).
/// - permissions: Optional permission list to attach to the presence document.
/// - metadata: Optional metadata payload.
public func upsertPresence(
status: String,
presenceId: String,
permissions: [String]? = nil,
metadata: [String: Any]? = nil
) throws {
var data: [String: Any] = [
"status": status,
"presenceId": presenceId,
]
if let permissions = permissions {
data["permissions"] = permissions
}
if let metadata = metadata {
data["metadata"] = metadata
}
presenceSync.sync {
pendingPresence = data
}
// Both subscribe() and upsertPresence() may need to open the socket.
// createSocket() is single-flight, so this is a no-op when one is
// already in flight or healthy. Fire-and-forget keeps upsertPresence's
// documented behavior of resolving once the payload is stored.
// Read socket state under `connectSync` since `createSocketLocked()`
// assigns `socketClient` from inside the single-flight task.
let needsSocket: Bool = connectSync.sync {
return socketClient == nil || !(socketClient?.isConnected ?? false)
}
if needsSocket {
Task { [weak self] in
do {
try await self?.createSocket()
} catch {
Realtime.logDiagnostic("Failed to open realtime socket for presence: \(error)")
}
}
}
// Opportunistic send for when the socket is already past `connected`.
// The appConnected gate inside flushPendingPresence keeps this a no-op
// until the application-level handshake completes.
try flushPendingPresence()
}
private func flushPendingPresence() throws {
var data: [String: Any]?
presenceSync.sync {
data = pendingPresence
}
let isAppConnected: Bool = connectSync.sync { appConnected }
guard let payloadData = data, let ws = socketClient, ws.isConnected, isAppConnected else {
return
}
let payload: [String: Any] = [
"type": "presence",
"data": payloadData
]
guard let jsonData = try? JSONSerialization.data(withJSONObject: payload),
let text = String(data: jsonData, encoding: .utf8) else {
throw AppwriteError(message: "Failed to encode presence payload")
}
ws.send(text: text)
}
public func subscribe(
channel: ChannelValue,
callback: @escaping (RealtimeResponseEvent) -> Void,
queries: [String] = []
) async throws -> RealtimeSubscription {
return try await subscribe(
channels: Set([channelToString(channel)]),
payloadType: String.self,
queries: queries,
callback: callback
)
}
public func subscribe(
channels: [ChannelValue],
callback: @escaping (RealtimeResponseEvent) -> Void,
queries: [String] = []
) async throws -> RealtimeSubscription {
return try await subscribe(
channels: Set(channels.map { channelToString($0) }),
payloadType: String.self,
queries: queries,
callback: callback
)
}
public func subscribe<T : Codable>(
channel: ChannelValue,
payloadType: T.Type,
callback: @escaping (RealtimeResponseEvent) -> Void,
queries: [String] = []
) async throws -> RealtimeSubscription {
return try await subscribe(
channels: Set([channelToString(channel)]),
payloadType: T.self,
queries: queries,
callback: callback
)
}
public func subscribe<T : Codable>(
channels: [ChannelValue],
payloadType: T.Type,
callback: @escaping (RealtimeResponseEvent) -> Void,
queries: [String] = []
) async throws -> RealtimeSubscription {
return try await subscribe(
channels: Set(channels.map { channelToString($0) }),
payloadType: T.self,
queries: queries,
callback: callback
)
}
public func subscribe<T : Codable>(
channels: Set<String>,
payloadType: T.Type,
queries: [String] = [],
callback: @escaping (RealtimeResponseEvent) -> Void
) async throws -> RealtimeSubscription {
let subscriptionId: String = try subscriptionsSync.sync {
let id = try generateUniqueSubscriptionIdLocked()
activeSubscriptions[id] = RealtimeCallback(
for: channels,
queries: queries,
with: callback
)
enqueuePendingSubscribeLocked(subscriptionId: id)
return id
}
connectSync.sync {
subCallDepth += 1
}
defer {
connectSync.sync {
self.subCallDepth -= 1
}
}
try await Task.sleep(nanoseconds: UInt64(DEBOUNCE_NANOS))
if self.subCallDepth == 1 {
if let ws = self.socketClient, ws.isConnected {
self.sendPendingSubscribes()
} else {
try await self.createSocket()
}
}
return RealtimeSubscription(
unsubscribe: { [weak self] in
guard let self = self else { return }
let removed: Bool = self.subscriptionsSync.sync {
guard self.activeSubscriptions[subscriptionId] != nil else { return false }
self.activeSubscriptions[subscriptionId] = nil
self.pendingSubscribes[subscriptionId] = nil
return true
}
if removed {
self.sendUnsubscribeMessage([subscriptionId])
}
},
update: { [weak self] changes in
guard let self = self else { return }
let didUpdate: Bool = self.subscriptionsSync.sync {
guard let subscription = self.activeSubscriptions[subscriptionId] else {
return false
}
if let nextChannels = changes.channels {
subscription.channels = Set(nextChannels.map { self.channelToString($0) })
}
if let nextQueries = changes.queries {
subscription.queries = nextQueries
}
self.enqueuePendingSubscribeLocked(subscriptionId: subscriptionId)
return true
}
guard didUpdate else { return }
self.connectSync.sync {
self.subCallDepth += 1
}
defer {
self.connectSync.sync {
self.subCallDepth -= 1
}
}
try await Task.sleep(nanoseconds: UInt64(self.DEBOUNCE_NANOS))
if self.subCallDepth == 1 {
if let ws = self.socketClient, ws.isConnected {
self.sendPendingSubscribes()
} else {
try await self.createSocket()
}
}
},
close: { [weak self] in
guard let self = self else { return }
let (removed, becameEmpty): (Bool, Bool) = self.subscriptionsSync.sync {
let wasPresent = self.activeSubscriptions[subscriptionId] != nil
if wasPresent {
self.activeSubscriptions[subscriptionId] = nil
self.pendingSubscribes[subscriptionId] = nil
}
return (wasPresent, self.activeSubscriptions.isEmpty)
}
if removed {
self.sendUnsubscribeMessage([subscriptionId])
}
if becameEmpty {
self.reconnect = false
try await self.closeSocket()
}
}
)
}
}
extension Realtime: WebSocketClientDelegate {
public func onOpen(channel: NIO.Channel) {
self.reconnectAttempts = 0
onOpenCallbacks.forEach { $0() }
startHeartbeat()
}
private func handleResponseConnected(from json: [String: Any]) {
guard json["data"] is [String: Any] else {
return
}
subscriptionsSync.sync {
for subscriptionId in activeSubscriptions.keys {
enqueuePendingSubscribeLocked(subscriptionId: subscriptionId)
}
}
connectSync.sync { appConnected = true }
sendPendingSubscribes()
try? flushPendingPresence()
}
public func onMessage(text: String) {
let data = Data(text.utf8)
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let type = json["type"] as? String else {
return
}
switch type {
case TYPE_ERROR:
do {
try handleResponseError(from: json)
} catch {
onErrorCallbacks.forEach { $0(error, nil) }
}
case "connected":
handleResponseConnected(from: json)
case TYPE_EVENT:
handleResponseEvent(from: json)
case TYPE_PONG:
break // Handle pong response if needed
default:
break
}
}
public func onClose(channel: NIO.Channel, data: Data) async throws {
connectSync.sync { appConnected = false }
stopHeartbeat()
onCloseCallbacks.forEach { $0() }
if (!reconnect) {
reconnect = true
return
}
let timeout = getTimeout()
Realtime.logDiagnostic("Realtime disconnected. Re-connecting in \(timeout / 1000) seconds.")
try await Task.sleep(nanoseconds: UInt64(timeout * 1_000_000))
self.reconnectAttempts += 1
try await self.createSocket()
}
public func onError(error: Swift.Error?, status: HTTPResponseStatus?) {
stopHeartbeat()
Realtime.logDiagnostic(error?.localizedDescription ?? "Unknown error")
onErrorCallbacks.forEach { $0(error, status) }
}
func handleResponseError(from json: [String: Any]) throws {
let message = json["message"] as? String ?? "Unknown error"
let error = AppwriteError(message: message)
throw error
}
func handleResponseEvent(from json: [String: Any]) {
guard let data = json["data"] as? [String: Any],
let channels = data["channels"] as? [String],
let events = data["events"] as? [String],
let payload = data["payload"] as? [String: Any],
let subscriptions = data["subscriptions"] as? [String] else {
return
}
guard subscriptions.count > 0 else {
return
}
for subscriptionId in subscriptions {
let subscription: RealtimeCallback? = subscriptionsSync.sync {
activeSubscriptions[subscriptionId]
}
if let subscription = subscription {
let response = RealtimeResponseEvent(
events: events,
channels: channels,
timestamp: data["timestamp"] as? String ?? "",
payload: payload
)
subscription.callback(response)
}
}
}
}