@@ -117,13 +117,18 @@ public final class RealtimeClientV2: Sendable {
117
117
wsTransport: @escaping WebSocketTransport ,
118
118
http: any HTTPClientType
119
119
) {
120
+ var options = options
121
+ if options. headers [ . xClientInfo] == nil {
122
+ options. headers [ . xClientInfo] = " realtime-swift/ \( version) "
123
+ }
124
+
120
125
self . url = url
121
126
self . options = options
122
127
self . wsTransport = wsTransport
123
128
self . http = http
124
129
apikey = options. apikey
125
130
126
- mutableState. withValue {
131
+ mutableState. withValue { [ options ] in
127
132
if let accessToken = options. headers [ . authorization] ? . split ( separator: " " ) . last {
128
133
$0. accessToken = String ( accessToken)
129
134
} else {
@@ -353,15 +358,15 @@ public final class RealtimeClientV2: Sendable {
353
358
if Task . isCancelled {
354
359
break
355
360
}
356
- self ? . sendHeartbeat ( )
361
+ await self ? . sendHeartbeat ( )
357
362
}
358
363
}
359
364
mutableState. withValue {
360
365
$0. heartbeatTask = heartbeatTask
361
366
}
362
367
}
363
368
364
- private func sendHeartbeat( ) {
369
+ private func sendHeartbeat( ) async {
365
370
let pendingHeartbeatRef : String ? = mutableState. withValue {
366
371
if $0. pendingHeartbeatRef != nil {
367
372
$0. pendingHeartbeatRef = nil
@@ -383,6 +388,7 @@ public final class RealtimeClientV2: Sendable {
383
388
payload: [ : ]
384
389
)
385
390
)
391
+ await setAuth ( )
386
392
} else {
387
393
options. logger? . debug ( " Heartbeat timeout " )
388
394
reconnect ( )
@@ -416,21 +422,13 @@ public final class RealtimeClientV2: Sendable {
416
422
/// On callback used, it will set the value of the token internal to the client.
417
423
/// - Parameter token: A JWT string to override the token set on the client.
418
424
public func setAuth( _ token: String ? = nil ) async {
419
- var token = token
420
-
421
- if token == nil {
422
- token = try ? await options. accessToken ? ( )
423
- }
425
+ var tokenToSend = token
424
426
425
- if token == nil {
426
- token = mutableState . accessToken
427
+ if tokenToSend == nil {
428
+ tokenToSend = try ? await options . accessToken ? ( )
427
429
}
428
430
429
- if let token, let payload = JWT . decodePayload ( token) ,
430
- let exp = payload [ " exp " ] as? TimeInterval , exp < Date ( ) . timeIntervalSince1970
431
- {
432
- options. logger? . warning (
433
- " InvalidJWTToken: Invalid value for JWT claim \" exp \" with value \( exp) " )
431
+ guard tokenToSend != mutableState. accessToken else {
434
432
return
435
433
}
436
434
0 commit comments