Skip to content

Commit 2cd5083

Browse files
committed
fix(realtime): Set default heartbeat interval to 25s (#667)
* fix * fix(realtime): Set default heartbeat interval to 25s
1 parent a0f3d41 commit 2cd5083

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

Sources/Realtime/Types.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ public struct RealtimeClientOptions: Sendable {
2121
var timeoutInterval: TimeInterval
2222
var disconnectOnSessionLoss: Bool
2323
var connectOnSubscribe: Bool
24+
25+
/// Sets the log level for Realtime
26+
var logLevel: LogLevel?
2427
var fetch: (@Sendable (_ request: URLRequest) async throws -> (Data, URLResponse))?
2528
package var accessToken: (@Sendable () async throws -> String?)?
2629
package var logger: (any SupabaseLogger)?
2730

28-
public static let defaultHeartbeatInterval: TimeInterval = 15
31+
public static let defaultHeartbeatInterval: TimeInterval = 25
2932
public static let defaultReconnectDelay: TimeInterval = 7
3033
public static let defaultTimeoutInterval: TimeInterval = 10
3134
public static let defaultDisconnectOnSessionLoss = true
@@ -38,6 +41,7 @@ public struct RealtimeClientOptions: Sendable {
3841
timeoutInterval: TimeInterval = Self.defaultTimeoutInterval,
3942
disconnectOnSessionLoss: Bool = Self.defaultDisconnectOnSessionLoss,
4043
connectOnSubscribe: Bool = Self.defaultConnectOnSubscribe,
44+
logLevel: LogLevel? = nil,
4145
fetch: (@Sendable (_ request: URLRequest) async throws -> (Data, URLResponse))? = nil,
4246
accessToken: (@Sendable () async throws -> String?)? = nil,
4347
logger: (any SupabaseLogger)? = nil
@@ -48,6 +52,7 @@ public struct RealtimeClientOptions: Sendable {
4852
self.timeoutInterval = timeoutInterval
4953
self.disconnectOnSessionLoss = disconnectOnSessionLoss
5054
self.connectOnSubscribe = connectOnSubscribe
55+
self.logLevel = logLevel
5156
self.fetch = fetch
5257
self.accessToken = accessToken
5358
self.logger = logger
@@ -85,7 +90,6 @@ extension HTTPField.Name {
8590
static let apiKey = Self("apiKey")!
8691
}
8792

88-
8993
/// Represents the different events that can be sent through
9094
/// a channel regarding a Channel's lifecycle.
9195
public enum ChannelEvent {
@@ -103,5 +107,9 @@ public enum ChannelEvent {
103107
public static let postgresChanges = "postgres_changes"
104108

105109
public static let heartbeat = "heartbeat"
110+
}
106111

112+
/// Log level for Realtime.
113+
public enum LogLevel: String, Sendable {
114+
case info, warn, error
107115
}

Sources/TestHelpers/MockExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import InlineSnapshotTesting
1212
extension Mock {
1313
package func snapshotRequest(
1414
message: @autoclosure () -> String = "",
15-
record isRecording: Bool? = nil,
15+
record isRecording: SnapshotTestingConfiguration.Record? = nil,
1616
timeout: TimeInterval = 5,
1717
syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(),
1818
matches expected: (() -> String)? = nil,

Supabase.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/RealtimeTests/RealtimeTests.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import XCTest
1414

1515
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
1616
final class RealtimeTests: XCTestCase {
17-
let url = URL(string: "https://localhost:54321/realtime/v1")!
17+
let url = URL(string: "http://localhost:54321/realtime/v1")!
1818
let apiKey = "anon.api.key"
1919

2020
override func invokeTest() {
@@ -49,7 +49,7 @@ final class RealtimeTests: XCTestCase {
4949
"custom.access.token"
5050
}
5151
),
52-
wsTransport: { self.client },
52+
wsTransport: { _, _ in self.client },
5353
http: http
5454
)
5555
}
@@ -60,6 +60,30 @@ final class RealtimeTests: XCTestCase {
6060
super.tearDown()
6161
}
6262

63+
func test_transport() async {
64+
let client = RealtimeClientV2(
65+
url: url,
66+
options: RealtimeClientOptions(
67+
headers: ["apikey": apiKey],
68+
logLevel: .warn,
69+
accessToken: {
70+
"custom.access.token"
71+
}
72+
),
73+
wsTransport: { url, headers in
74+
assertInlineSnapshot(of: url, as: .description) {
75+
"""
76+
ws://localhost:54321/realtime/v1/websocket?apikey=anon.api.key&vsn=1.0.0&log_level=warn
77+
"""
78+
}
79+
return FakeWebSocket.fakes().0
80+
},
81+
http: http
82+
)
83+
84+
await client.connect()
85+
}
86+
6387
func testBehavior() async throws {
6488
let channel = sut.channel("public:messages")
6589
var subscriptions: Set<ObservationToken> = []
@@ -352,7 +376,7 @@ final class RealtimeTests: XCTestCase {
352376
let request = await http.receivedRequests.last
353377
assertInlineSnapshot(of: request?.urlRequest, as: .raw(pretty: true)) {
354378
"""
355-
POST https://localhost:54321/realtime/v1/api/broadcast
379+
POST http://localhost:54321/realtime/v1/api/broadcast
356380
Authorization: Bearer custom.access.token
357381
Content-Type: application/json
358382
apiKey: anon.api.key

Tests/RealtimeTests/_PushTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class _PushTests: XCTestCase {
3434
options: RealtimeClientOptions(
3535
headers: ["apiKey": "apikey"]
3636
),
37-
wsTransport: { client },
37+
wsTransport: { _, _ in client },
3838
http: HTTPClientMock()
3939
)
4040
}

0 commit comments

Comments
 (0)