Skip to content

Commit

Permalink
fix(realtime): Set default heartbeat interval to 25s (#667)
Browse files Browse the repository at this point in the history
* fix

* fix(realtime): Set default heartbeat interval to 25s
  • Loading branch information
grdsdev committed Feb 19, 2025
1 parent a0f3d41 commit 2cd5083
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
12 changes: 10 additions & 2 deletions Sources/Realtime/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ public struct RealtimeClientOptions: Sendable {
var timeoutInterval: TimeInterval
var disconnectOnSessionLoss: Bool
var connectOnSubscribe: Bool

/// Sets the log level for Realtime
var logLevel: LogLevel?
var fetch: (@Sendable (_ request: URLRequest) async throws -> (Data, URLResponse))?
package var accessToken: (@Sendable () async throws -> String?)?
package var logger: (any SupabaseLogger)?

public static let defaultHeartbeatInterval: TimeInterval = 15
public static let defaultHeartbeatInterval: TimeInterval = 25
public static let defaultReconnectDelay: TimeInterval = 7
public static let defaultTimeoutInterval: TimeInterval = 10
public static let defaultDisconnectOnSessionLoss = true
Expand All @@ -38,6 +41,7 @@ public struct RealtimeClientOptions: Sendable {
timeoutInterval: TimeInterval = Self.defaultTimeoutInterval,
disconnectOnSessionLoss: Bool = Self.defaultDisconnectOnSessionLoss,
connectOnSubscribe: Bool = Self.defaultConnectOnSubscribe,
logLevel: LogLevel? = nil,
fetch: (@Sendable (_ request: URLRequest) async throws -> (Data, URLResponse))? = nil,
accessToken: (@Sendable () async throws -> String?)? = nil,
logger: (any SupabaseLogger)? = nil
Expand All @@ -48,6 +52,7 @@ public struct RealtimeClientOptions: Sendable {
self.timeoutInterval = timeoutInterval
self.disconnectOnSessionLoss = disconnectOnSessionLoss
self.connectOnSubscribe = connectOnSubscribe
self.logLevel = logLevel
self.fetch = fetch
self.accessToken = accessToken
self.logger = logger
Expand Down Expand Up @@ -85,7 +90,6 @@ extension HTTPField.Name {
static let apiKey = Self("apiKey")!
}


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

public static let heartbeat = "heartbeat"
}

/// Log level for Realtime.
public enum LogLevel: String, Sendable {
case info, warn, error
}
2 changes: 1 addition & 1 deletion Sources/TestHelpers/MockExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import InlineSnapshotTesting
extension Mock {
package func snapshotRequest(
message: @autoclosure () -> String = "",
record isRecording: Bool? = nil,
record isRecording: SnapshotTestingConfiguration.Record? = nil,
timeout: TimeInterval = 5,
syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(),
matches expected: (() -> String)? = nil,
Expand Down
28 changes: 14 additions & 14 deletions Supabase.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-asn1.git",
"state" : {
"revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6",
"version" : "1.3.0"
"revision" : "ae33e5941bb88d88538d0a6b19ca0b01e6c76dcf",
"version" : "1.3.1"
}
},
{
"identity" : "swift-case-paths",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-case-paths",
"state" : {
"revision" : "bc92c4b27f9a84bfb498cdbfdf35d5a357e9161f",
"version" : "1.5.6"
"revision" : "19b7263bacb9751f151ec0c93ec816fe1ef67c7b",
"version" : "1.6.1"
}
},
{
Expand All @@ -95,17 +95,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "163409ef7dae9d960b87f34b51587b6609a76c1f",
"version" : "1.3.0"
"revision" : "82a4ae7170d98d8538ec77238b7eb8e7199ef2e8",
"version" : "1.3.1"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "ff0f781cf7c6a22d52957e50b104f5768b50c779",
"version" : "3.10.0"
"revision" : "45305d32cfb830faebcaa9a7aea66ad342637518",
"version" : "3.11.1"
}
},
{
Expand All @@ -131,17 +131,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-identified-collections.git",
"state" : {
"revision" : "2f5ab6e091dd032b63dacbda052405756010dc3b",
"version" : "1.1.0"
"revision" : "322d9ffeeba85c9f7c4984b39422ec7cc3c56597",
"version" : "1.1.1"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "42a086182681cf661f5c47c9b7dc3931de18c6d7",
"version" : "1.17.6"
"revision" : "b2d4cb30735f4fbc3a01963a9c658336dd21e9ba",
"version" : "1.18.1"
}
},
{
Expand All @@ -167,8 +167,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "a3f634d1a409c7979cabc0a71b3f26ffa9fc8af1",
"version" : "1.4.3"
"revision" : "b444594f79844b0d6d76d70fbfb3f7f71728f938",
"version" : "1.5.1"
}
}
],
Expand Down
30 changes: 27 additions & 3 deletions Tests/RealtimeTests/RealtimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest

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

override func invokeTest() {
Expand Down Expand Up @@ -49,7 +49,7 @@ final class RealtimeTests: XCTestCase {
"custom.access.token"
}
),
wsTransport: { self.client },
wsTransport: { _, _ in self.client },
http: http
)
}
Expand All @@ -60,6 +60,30 @@ final class RealtimeTests: XCTestCase {
super.tearDown()
}

func test_transport() async {
let client = RealtimeClientV2(
url: url,
options: RealtimeClientOptions(
headers: ["apikey": apiKey],
logLevel: .warn,
accessToken: {
"custom.access.token"
}
),
wsTransport: { url, headers in
assertInlineSnapshot(of: url, as: .description) {
"""
ws://localhost:54321/realtime/v1/websocket?apikey=anon.api.key&vsn=1.0.0&log_level=warn
"""
}
return FakeWebSocket.fakes().0
},
http: http
)

await client.connect()
}

func testBehavior() async throws {
let channel = sut.channel("public:messages")
var subscriptions: Set<ObservationToken> = []
Expand Down Expand Up @@ -352,7 +376,7 @@ final class RealtimeTests: XCTestCase {
let request = await http.receivedRequests.last
assertInlineSnapshot(of: request?.urlRequest, as: .raw(pretty: true)) {
"""
POST https://localhost:54321/realtime/v1/api/broadcast
POST http://localhost:54321/realtime/v1/api/broadcast
Authorization: Bearer custom.access.token
Content-Type: application/json
apiKey: anon.api.key
Expand Down
2 changes: 1 addition & 1 deletion Tests/RealtimeTests/_PushTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class _PushTests: XCTestCase {
options: RealtimeClientOptions(
headers: ["apiKey": "apikey"]
),
wsTransport: { client },
wsTransport: { _, _ in client },
http: HTTPClientMock()
)
}
Expand Down

0 comments on commit 2cd5083

Please sign in to comment.