Skip to content

Commit 237ac9c

Browse files
authored
fix(realtime): make Apikey required (#760)
1 parent e072a58 commit 237ac9c

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

Sources/Realtime/RealtimeChannelV2.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ public final class RealtimeChannelV2: Sendable, RealtimeChannelProtocol {
499499
)
500500
Task {
501501
await unsubscribe()
502-
await subscribe()
502+
try? await subscribeWithError()
503503
}
504504
}
505505

Sources/Realtime/RealtimeClientV2.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public final class RealtimeClientV2: Sendable, RealtimeClientProtocol {
5353
let wsTransport: WebSocketTransport
5454
let mutableState = LockIsolated(MutableState())
5555
let http: any HTTPClientType
56-
let apikey: String?
56+
let apikey: String
5757

5858
var conn: (any WebSocket)? {
5959
mutableState.conn
@@ -157,13 +157,13 @@ public final class RealtimeClientV2: Sendable, RealtimeClientProtocol {
157157
self.options = options
158158
self.wsTransport = wsTransport
159159
self.http = http
160-
apikey = options.apikey
160+
161+
precondition(options.apikey != nil, "API key is required to connect to Realtime")
162+
apikey = options.apikey!
161163

162164
mutableState.withValue { [options] in
163165
if let accessToken = options.headers[.authorization]?.split(separator: " ").last {
164166
$0.accessToken = String(accessToken)
165-
} else {
166-
$0.accessToken = options.apikey
167167
}
168168
}
169169
}
@@ -360,7 +360,7 @@ public final class RealtimeClientV2: Sendable, RealtimeClientProtocol {
360360
private func rejoinChannels() {
361361
Task {
362362
for channel in channels.values {
363-
await channel.subscribe()
363+
try? await channel.subscribeWithError()
364364
}
365365
}
366366
}

Tests/RealtimeTests/RealtimeChannelTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class RealtimeChannelTests: XCTestCase {
2222
),
2323
socket: RealtimeClientV2(
2424
url: URL(string: "https://localhost:54321/realtime/v1")!,
25-
options: RealtimeClientOptions()
25+
options: RealtimeClientOptions(headers: ["apikey": "test-key"])
2626
),
2727
logger: nil
2828
)
@@ -161,8 +161,8 @@ final class RealtimeChannelTests: XCTestCase {
161161
XCTAssertTrue(channel.callbackManager.callbacks.contains(where: { $0.isPresence }))
162162

163163
// Start subscription process
164-
let subscribeTask = Task {
165-
await channel.subscribe()
164+
Task {
165+
try? await channel.subscribeWithError()
166166
}
167167

168168
// Wait for the join message to be sent

0 commit comments

Comments
 (0)