@@ -117,13 +117,18 @@ public final class RealtimeClientV2: Sendable {
117117 wsTransport: @escaping WebSocketTransport ,
118118 http: any HTTPClientType
119119 ) {
120+ var options = options
121+ if options. headers [ . xClientInfo] == nil {
122+ options. headers [ . xClientInfo] = " realtime-swift/ \( version) "
123+ }
124+
120125 self . url = url
121126 self . options = options
122127 self . wsTransport = wsTransport
123128 self . http = http
124129 apikey = options. apikey
125130
126- mutableState. withValue {
131+ mutableState. withValue { [ options ] in
127132 if let accessToken = options. headers [ . authorization] ? . split ( separator: " " ) . last {
128133 $0. accessToken = String ( accessToken)
129134 } else {
@@ -353,15 +358,15 @@ public final class RealtimeClientV2: Sendable {
353358 if Task . isCancelled {
354359 break
355360 }
356- self ? . sendHeartbeat ( )
361+ await self ? . sendHeartbeat ( )
357362 }
358363 }
359364 mutableState. withValue {
360365 $0. heartbeatTask = heartbeatTask
361366 }
362367 }
363368
364- private func sendHeartbeat( ) {
369+ private func sendHeartbeat( ) async {
365370 let pendingHeartbeatRef : String ? = mutableState. withValue {
366371 if $0. pendingHeartbeatRef != nil {
367372 $0. pendingHeartbeatRef = nil
@@ -383,6 +388,7 @@ public final class RealtimeClientV2: Sendable {
383388 payload: [ : ]
384389 )
385390 )
391+ await setAuth ( )
386392 } else {
387393 options. logger? . debug ( " Heartbeat timeout " )
388394 reconnect ( )
@@ -416,21 +422,13 @@ public final class RealtimeClientV2: Sendable {
416422 /// On callback used, it will set the value of the token internal to the client.
417423 /// - Parameter token: A JWT string to override the token set on the client.
418424 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
424426
425- if token == nil {
426- token = mutableState . accessToken
427+ if tokenToSend == nil {
428+ tokenToSend = try ? await options . accessToken ? ( )
427429 }
428430
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 {
434432 return
435433 }
436434
0 commit comments