From ac43a8bfad012321288338cc639fca09275f5887 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 21 Oct 2025 12:00:31 +0530 Subject: [PATCH 1/4] Add error and close callbacks to Swift Realtime service This change adds support for custom error and close event handlers in the Swift Realtime implementation. Developers can now register callbacks to be notified when connection errors occur or when the connection closes, enabling better error handling and connection state management in applications. --- .../swift/Sources/Services/Realtime.swift.twig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/templates/swift/Sources/Services/Realtime.swift.twig b/templates/swift/Sources/Services/Realtime.swift.twig index 88b06f9ab5..69ac2da072 100644 --- a/templates/swift/Sources/Services/Realtime.swift.twig +++ b/templates/swift/Sources/Services/Realtime.swift.twig @@ -22,6 +22,17 @@ open class Realtime : Service { private var reconnectAttempts = 0 private var subscriptionsCounter = 0 private var reconnect = true + + private var onErrorCallback: ((Swift.Error?, HTTPResponseStatus?) -> Void)? + private var onCloseCallback: (() -> Void)? + + public func onError(_ callback: @escaping (Swift.Error?, HTTPResponseStatus?) -> Void) { + self.onErrorCallback = callback + } + + public func onClose(_ callback: @escaping () -> Void) { + self.onCloseCallback = callback + } private func startHeartbeat() { stopHeartbeat() @@ -211,6 +222,8 @@ extension Realtime: WebSocketClientDelegate { public func onClose(channel: Channel, data: Data) async throws { stopHeartbeat() + onCloseCallback?() + if (!reconnect) { reconnect = true return @@ -230,6 +243,8 @@ extension Realtime: WebSocketClientDelegate { public func onError(error: Swift.Error?, status: HTTPResponseStatus?) { stopHeartbeat() print(error?.localizedDescription ?? "Unknown error") + + onErrorCallback?(error, status) } func handleResponseError(from json: [String: Any]) throws { From cc46d552b62a2a2c8c1a00111736680a63213178 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 21 Oct 2025 12:08:03 +0530 Subject: [PATCH 2/4] Update templates/swift/Sources/Services/Realtime.swift.twig Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- templates/swift/Sources/Services/Realtime.swift.twig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/swift/Sources/Services/Realtime.swift.twig b/templates/swift/Sources/Services/Realtime.swift.twig index 69ac2da072..946c3a87be 100644 --- a/templates/swift/Sources/Services/Realtime.swift.twig +++ b/templates/swift/Sources/Services/Realtime.swift.twig @@ -23,15 +23,15 @@ open class Realtime : Service { private var subscriptionsCounter = 0 private var reconnect = true - private var onErrorCallback: ((Swift.Error?, HTTPResponseStatus?) -> Void)? - private var onCloseCallback: (() -> Void)? + private var onErrorCallbacks: [((Swift.Error?, HTTPResponseStatus?) -> Void)] = [] + private var onCloseCallbacks: [(() -> Void)] = [] public func onError(_ callback: @escaping (Swift.Error?, HTTPResponseStatus?) -> Void) { - self.onErrorCallback = callback + self.onErrorCallbacks.append(callback) } public func onClose(_ callback: @escaping () -> Void) { - self.onCloseCallback = callback + self.onCloseCallbacks.append(callback) } private func startHeartbeat() { From c88c9a9ed535c87412e372795f11ddad410fb4c1 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 21 Oct 2025 12:08:26 +0530 Subject: [PATCH 3/4] Update templates/swift/Sources/Services/Realtime.swift.twig Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- templates/swift/Sources/Services/Realtime.swift.twig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/templates/swift/Sources/Services/Realtime.swift.twig b/templates/swift/Sources/Services/Realtime.swift.twig index 946c3a87be..dc63e6fd4f 100644 --- a/templates/swift/Sources/Services/Realtime.swift.twig +++ b/templates/swift/Sources/Services/Realtime.swift.twig @@ -222,9 +222,8 @@ extension Realtime: WebSocketClientDelegate { public func onClose(channel: Channel, data: Data) async throws { stopHeartbeat() - onCloseCallback?() - if (!reconnect) { + onCloseCallback?() reconnect = true return } From e278eb25625532efa46fd70c0216a5c0ce6817a8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 21 Oct 2025 12:17:32 +0530 Subject: [PATCH 4/4] fix: invocation --- templates/swift/Sources/Services/Realtime.swift.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/swift/Sources/Services/Realtime.swift.twig b/templates/swift/Sources/Services/Realtime.swift.twig index dc63e6fd4f..4558f54bbc 100644 --- a/templates/swift/Sources/Services/Realtime.swift.twig +++ b/templates/swift/Sources/Services/Realtime.swift.twig @@ -223,7 +223,7 @@ extension Realtime: WebSocketClientDelegate { stopHeartbeat() if (!reconnect) { - onCloseCallback?() + onCloseCallbacks.forEach { $0() } reconnect = true return } @@ -243,7 +243,7 @@ extension Realtime: WebSocketClientDelegate { stopHeartbeat() print(error?.localizedDescription ?? "Unknown error") - onErrorCallback?(error, status) + onErrorCallbacks.forEach { $0(error, status) } } func handleResponseError(from json: [String: Any]) throws {