22import ConcurrencyExtras
33import Foundation
44@_exported import Functions
5+ import HTTPTypes
56import Helpers
67import IssueReporting
78@_exported import PostgREST
89@_exported import Realtime
910@_exported import Storage
10- import HTTPTypes
1111
1212#if canImport(FoundationNetworking)
1313 import FoundationNetworking
@@ -33,10 +33,11 @@ public final class SupabaseClient: Sendable {
3333 /// Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies.
3434 public var auth : AuthClient {
3535 if options. auth. accessToken != nil {
36- reportIssue ( """
37- Supabase Client is configured with the auth.accessToken option,
38- accessing supabase.auth is not possible.
39- """ )
36+ reportIssue (
37+ """
38+ Supabase Client is configured with the auth.accessToken option,
39+ accessing supabase.auth is not possible.
40+ """ )
4041 }
4142 return _auth
4243 }
@@ -80,7 +81,14 @@ public final class SupabaseClient: Sendable {
8081 let _realtime : UncheckedSendable < RealtimeClient >
8182
8283 /// Realtime client for Supabase
83- public let realtimeV2 : RealtimeClientV2
84+ public var realtimeV2 : RealtimeClientV2 {
85+ mutableState. withValue {
86+ if $0. realtime == nil {
87+ $0. realtime = _initRealtimeClient ( )
88+ }
89+ return $0. realtime!
90+ }
91+ }
8492
8593 /// Supabase Functions allows you to deploy and invoke edge functions.
8694 public var functions : FunctionsClient {
@@ -112,6 +120,7 @@ public final class SupabaseClient: Sendable {
112120 var storage : SupabaseStorageClient ?
113121 var rest : PostgrestClient ?
114122 var functions : FunctionsClient ?
123+ var realtime : RealtimeClientV2 ?
115124
116125 var changedAccessToken : String ?
117126 }
@@ -189,18 +198,6 @@ public final class SupabaseClient: Sendable {
189198 )
190199 )
191200
192- var realtimeOptions = options. realtime
193- realtimeOptions. headers. merge ( with: _headers)
194-
195- if realtimeOptions. logger == nil {
196- realtimeOptions. logger = options. global. logger
197- }
198-
199- realtimeV2 = RealtimeClientV2 (
200- url: supabaseURL. appendingPathComponent ( " /realtime/v1 " ) ,
201- options: realtimeOptions
202- )
203-
204201 if options. auth. accessToken == nil {
205202 listenForAuthEvents ( )
206203 }
@@ -351,11 +348,12 @@ public final class SupabaseClient: Sendable {
351348 }
352349
353350 private func adapt( request: URLRequest ) async -> URLRequest {
354- let token : String ? = if let accessToken = options. auth. accessToken {
355- try ? await accessToken ( )
356- } else {
357- try ? await auth. session. accessToken
358- }
351+ let token : String ? =
352+ if let accessToken = options. auth. accessToken {
353+ try ? await accessToken ( )
354+ } else {
355+ try ? await auth. session. accessToken
356+ }
359357
360358 var request = request
361359 if let token {
@@ -377,7 +375,9 @@ public final class SupabaseClient: Sendable {
377375
378376 private func handleTokenChanged( event: AuthChangeEvent , session: Session ? ) async {
379377 let accessToken : String ? = mutableState. withValue {
380- if [ . initialSession, . signedIn, . tokenRefreshed] . contains ( event) , $0. changedAccessToken != session? . accessToken {
378+ if [ . initialSession, . signedIn, . tokenRefreshed] . contains ( event) ,
379+ $0. changedAccessToken != session? . accessToken
380+ {
381381 $0. changedAccessToken = session? . accessToken
382382 return session? . accessToken ?? supabaseKey
383383 }
@@ -393,4 +393,25 @@ public final class SupabaseClient: Sendable {
393393 realtime. setAuth ( accessToken)
394394 await realtimeV2. setAuth ( accessToken)
395395 }
396+
397+ private func _initRealtimeClient( ) -> RealtimeClientV2 {
398+ var realtimeOptions = options. realtime
399+ realtimeOptions. headers. merge ( with: _headers)
400+
401+ if realtimeOptions. logger == nil {
402+ realtimeOptions. logger = options. global. logger
403+ }
404+
405+ if realtimeOptions. accessToken == nil {
406+ realtimeOptions. accessToken = { [ weak self] in
407+ guard let self else { return " " }
408+ return try await self . auth. session. accessToken
409+ }
410+ }
411+
412+ return RealtimeClientV2 (
413+ url: supabaseURL. appendingPathComponent ( " /realtime/v1 " ) ,
414+ options: realtimeOptions
415+ )
416+ }
396417}
0 commit comments