Skip to content

Commit 80cd6e8

Browse files
authored
docs: fix Realtime example (#167)
1 parent 6f033fb commit 80cd6e8

File tree

6 files changed

+39
-37
lines changed

6 files changed

+39
-37
lines changed

Examples/Examples.xcodeproj/project.pbxproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
790132E02B0C29080051B356 /* Supabase in Frameworks */ = {isa = PBXBuildFile; productRef = 790132DF2B0C29080051B356 /* Supabase */; };
1011
790308E92AEE7B4D003C4A98 /* RealtimeSampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 790308E82AEE7B4D003C4A98 /* RealtimeSampleApp.swift */; };
1112
790308EB2AEE7B4D003C4A98 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 790308EA2AEE7B4D003C4A98 /* ContentView.swift */; };
1213
790308ED2AEE7B4E003C4A98 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 790308EC2AEE7B4E003C4A98 /* Assets.xcassets */; };
1314
790308F02AEE7B4E003C4A98 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 790308EF2AEE7B4E003C4A98 /* Preview Assets.xcassets */; };
14-
790308F62AEE7B5B003C4A98 /* Realtime in Frameworks */ = {isa = PBXBuildFile; productRef = 790308F52AEE7B5B003C4A98 /* Realtime */; };
1515
793895CA2954ABFF0044F2B8 /* ExamplesApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793895C92954ABFF0044F2B8 /* ExamplesApp.swift */; };
1616
793895CC2954ABFF0044F2B8 /* RootView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793895CB2954ABFF0044F2B8 /* RootView.swift */; };
1717
793895CE2954AC000044F2B8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 793895CD2954AC000044F2B8 /* Assets.xcassets */; };
@@ -85,7 +85,7 @@
8585
isa = PBXFrameworksBuildPhase;
8686
buildActionMask = 2147483647;
8787
files = (
88-
790308F62AEE7B5B003C4A98 /* Realtime in Frameworks */,
88+
790132E02B0C29080051B356 /* Supabase in Frameworks */,
8989
);
9090
runOnlyForDeploymentPostprocessing = 0;
9191
};
@@ -233,7 +233,7 @@
233233
);
234234
name = RealtimeSample;
235235
packageProductDependencies = (
236-
790308F52AEE7B5B003C4A98 /* Realtime */,
236+
790132DF2B0C29080051B356 /* Supabase */,
237237
);
238238
productName = RealtimeSample;
239239
productReference = 790308E62AEE7B4D003C4A98 /* RealtimeSample.app */;
@@ -800,9 +800,9 @@
800800
/* End XCRemoteSwiftPackageReference section */
801801

802802
/* Begin XCSwiftPackageProductDependency section */
803-
790308F52AEE7B5B003C4A98 /* Realtime */ = {
803+
790132DF2B0C29080051B356 /* Supabase */ = {
804804
isa = XCSwiftPackageProductDependency;
805-
productName = Realtime;
805+
productName = Supabase;
806806
};
807807
7956406C2955B3500088A06F /* SwiftUINavigation */ = {
808808
isa = XCSwiftPackageProductDependency;

Examples/RealtimeSample/ContentView.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,13 @@ struct ContentView: View {
5353
}
5454
.onAppear {
5555
createSubscription()
56-
57-
socket.connect()
58-
socket.onOpen {
59-
socketStatus = "OPEN"
60-
}
61-
socket.onClose {
62-
socketStatus = "CLOSE"
63-
}
64-
socket.onError { error, _ in
65-
socketStatus = "ERROR: \(error.localizedDescription)"
66-
}
6756
}
6857
}
6958

7059
func createSubscription() {
71-
publicSchema = socket.channel("public")
60+
supabase.realtime.connect()
61+
62+
publicSchema = supabase.realtime.channel("public")
7263
.on("postgres_changes", filter: ChannelFilter(event: "INSERT", schema: "public")) {
7364
inserts.append($0)
7465
}
@@ -94,6 +85,17 @@ struct ContentView: View {
9485
channelStatus = "ERROR"
9586
}
9687
}
88+
89+
supabase.realtime.connect()
90+
supabase.realtime.onOpen {
91+
socketStatus = "OPEN"
92+
}
93+
supabase.realtime.onClose {
94+
socketStatus = "CLOSE"
95+
}
96+
supabase.realtime.onError { error, _ in
97+
socketStatus = "ERROR: \(error.localizedDescription)"
98+
}
9799
}
98100

99101
func toggleSubscription() {

Examples/RealtimeSample/RealtimeSampleApp.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Guilherme Souza on 29/10/23.
66
//
77

8-
import Realtime
8+
import Supabase
99
import SwiftUI
1010

1111
@main
@@ -17,13 +17,11 @@ struct RealtimeSampleApp: App {
1717
}
1818
}
1919

20-
let socket: RealtimeClient = {
21-
let client = RealtimeClient(
22-
"https://PROJECT_ID.supabase.co/realtime/v1",
23-
params: [
24-
"apikey": "SUPABASE_ANON_KEY",
25-
]
20+
let supabase: SupabaseClient = {
21+
let client = SupabaseClient(
22+
supabaseURL: "https://project-id.supabase.co",
23+
supabaseKey: "anon key"
2624
)
27-
client.logger = { print($0) }
25+
client.realtime.logger = { print($0) }
2826
return client
2927
}()

Examples/UserManagement/Supabase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import Foundation
99
import Supabase
1010

1111
let supabase = SupabaseClient(
12-
supabaseURL: URL(string: "https://PROJECT_ID.supabase.co")!,
12+
supabaseURL: "https://PROJECT_ID.supabase.co",
1313
supabaseKey: "YOUR_SUPABASE_ANON_KEY"
1414
)

Sources/Supabase/SupabaseClient.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public final class SupabaseClient: @unchecked Sendable {
1616
let supabaseKey: String
1717
let storageURL: URL
1818
let databaseURL: URL
19-
let realtimeURL: URL
2019
let functionsURL: URL
2120

2221
/// Supabase Auth allows you to create and manage user sessions for access to data that is secured
@@ -41,11 +40,7 @@ public final class SupabaseClient: @unchecked Sendable {
4140
)
4241

4342
/// Realtime client for Supabase
44-
public private(set) lazy var realtime = RealtimeClient(
45-
realtimeURL.absoluteString,
46-
headers: defaultHeaders,
47-
params: defaultHeaders
48-
)
43+
public let realtime: RealtimeClient
4944

5045
/// Supabase Functions allows you to deploy and invoke edge functions.
5146
public private(set) lazy var functions = FunctionsClient(
@@ -69,17 +64,20 @@ public final class SupabaseClient: @unchecked Sendable {
6964
/// your project dashboard.
7065
/// - options: Custom options to configure client's behavior.
7166
public init(
72-
supabaseURL: URL,
67+
supabaseURL: String,
7368
supabaseKey: String,
7469
options: SupabaseClientOptions = .init()
7570
) {
71+
guard let supabaseURL = URL(string: supabaseURL) else {
72+
fatalError("Invalid supabaseURL: \(supabaseURL)")
73+
}
74+
7675
self.supabaseURL = supabaseURL
7776
self.supabaseKey = supabaseKey
7877
self.options = options
7978

8079
storageURL = supabaseURL.appendingPathComponent("/storage/v1")
8180
databaseURL = supabaseURL.appendingPathComponent("/rest/v1")
82-
realtimeURL = supabaseURL.appendingPathComponent("/realtime/v1")
8381
functionsURL = supabaseURL.appendingPathComponent("/functions/v1")
8482

8583
defaultHeaders = [
@@ -99,6 +97,12 @@ public final class SupabaseClient: @unchecked Sendable {
9997
}
10098
)
10199

100+
realtime = RealtimeClient(
101+
supabaseURL.appendingPathComponent("/realtime/v1").absoluteString,
102+
headers: defaultHeaders,
103+
params: defaultHeaders
104+
)
105+
102106
listenForAuthEvents()
103107
}
104108

Tests/SupabaseTests/SupabaseClientTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class SupabaseClientTests: XCTestCase {
2020
let customHeaders = ["header_field": "header_value"]
2121

2222
let client = SupabaseClient(
23-
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
23+
supabaseURL: "https://project-ref.supabase.co",
2424
supabaseKey: "ANON_KEY",
2525
options: SupabaseClientOptions(
2626
db: SupabaseClientOptions.DatabaseOptions(schema: customSchema),
@@ -34,10 +34,8 @@ final class SupabaseClientTests: XCTestCase {
3434

3535
XCTAssertEqual(client.supabaseURL.absoluteString, "https://project-ref.supabase.co")
3636
XCTAssertEqual(client.supabaseKey, "ANON_KEY")
37-
// XCTAssertEqual(client.authURL.absoluteString, "https://project-ref.supabase.co/auth/v1")
3837
XCTAssertEqual(client.storageURL.absoluteString, "https://project-ref.supabase.co/storage/v1")
3938
XCTAssertEqual(client.databaseURL.absoluteString, "https://project-ref.supabase.co/rest/v1")
40-
XCTAssertEqual(client.realtimeURL.absoluteString, "https://project-ref.supabase.co/realtime/v1")
4139
XCTAssertEqual(
4240
client.functionsURL.absoluteString,
4341
"https://project-ref.supabase.co/functions/v1"

0 commit comments

Comments
 (0)