Skip to content

Commit a65d04c

Browse files
Merge pull request #2 from mbarnach/main
Swiftification of the API
2 parents 242acdc + 9edd2f7 commit a65d04c

File tree

7 files changed

+305
-74
lines changed

7 files changed

+305
-74
lines changed

README.md

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ client.connect()
2020
**Socket Hooks**
2121

2222
```swift
23-
socket.onOpen {
23+
client.onOpen {
2424
print("Socket opened.")
2525
}
2626

27-
socket.onError { error in
27+
client.onError { error in
2828
print("Socket error: ", error.localizedDescription)
2929
}
3030

31-
socket.onClose {
31+
client.onClose {
3232
print("Socket closed")
3333
}
3434
```
@@ -41,10 +41,67 @@ Call `disconnect()` on the socket:
4141
client.disconnect()
4242
```
4343

44+
### Subscribe to topics
45+
46+
You can subscribe to all topic, or to specific schema parts.
47+
48+
* Listen to all database changes:
49+
50+
```swift
51+
let allChanges = client.channel(.all)
52+
allChanges.on(.all) { message in
53+
print(message)
54+
}
55+
allChanges.subscribe()
56+
// ...
57+
allChanges.unsubscribe()
58+
allChanges.off(.all)
59+
```
60+
61+
* Listen to a specific schema's changes:
62+
63+
```swift
64+
let allPublicInsertChanges = client.channel(.schema("public"))
65+
allPublicInsertChanges.on(.insert) { message in
66+
print(message)
67+
}
68+
allPublicInsertChanges.subscribe()
69+
// ...
70+
allPublicInsertChanges.unsubscribe()
71+
allPublicInsertChanges.off(.insert)
72+
```
73+
74+
* Listen to a specific table's changes:
75+
76+
```swift
77+
let allUsersUpdateChanges = client.channel(.table("users", schema: "public"))
78+
allUsersUpdateChanges.on(.update) { message in
79+
print(message)
80+
}
81+
allUsersUpdateChanges.subscribe()
82+
// ...
83+
allUsersUpdateChanges.unsubscribe()
84+
allUsersUpdateChanges.off(.update)
85+
```
86+
87+
* Listen to a specific column's value changes:
88+
89+
```swift
90+
let allUserId99Changes = client.channel(.column("id", value: "99", table: "users", schema: "public"))
91+
allUserId99Changes.on(.all){ message in
92+
print(message)
93+
}
94+
allUserId99Changes.subscribe()
95+
// ...
96+
allUserId99Changes.unsubscribe()
97+
allUserId99Changes.off(.all)
98+
```
99+
100+
44101
## Credits
45102

46-
- https://github.com/supabase/realtime-js
47-
- https://github.com/davidstump/SwiftPhoenixClient
103+
- https://github.com/supabase/realtime-js
104+
- https://github.com/davidstump/SwiftPhoenixClient
48105

49106
## License
50107

Sources/Realtime/Channel.swift

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Swift
2424
/// Container class of bindings to the channel
2525
struct Binding {
2626
// The event that the Binding is bound to
27-
let event: String
27+
let event: ChannelEvent
2828

2929
// The reference number of the Binding
3030
let ref: Int
@@ -57,8 +57,8 @@ struct Binding {
5757
import Foundation
5858

5959
public class Channel {
60-
/// The topic of the Channel. e.g. "rooms:friends"
61-
public let topic: String
60+
/// The topic of the Channel. e.g. `.table("rooms", "friends")`
61+
public let topic: ChannelTopic
6262

6363
/// The params sent when joining the channel
6464
public var params: [String: Any] {
@@ -100,7 +100,7 @@ public class Channel {
100100
/// - parameter topic: Topic of the Channel
101101
/// - parameter params: Optional. Parameters to send when joining.
102102
/// - parameter socket: Socket that the channel is a part of
103-
init(topic: String, params: [String: Any] = [:], socket: RealtimeClient) {
103+
init(topic: ChannelTopic, params: [String: Any] = [:], socket: RealtimeClient) {
104104
state = ChannelState.closed
105105
self.topic = topic
106106
self.params = params
@@ -217,7 +217,7 @@ public class Channel {
217217
// Perform when the join reply is received
218218
delegateOn(ChannelEvent.reply, to: self) { (self, message) in
219219
// Trigger bindings
220-
self.trigger(event: self.replyEventName(message.ref),
220+
self.trigger(event: ChannelEvent.channelReply(message.ref),
221221
payload: message.payload,
222222
ref: message.ref,
223223
joinRef: message.joinRef)
@@ -340,13 +340,13 @@ public class Channel {
340340
/// Example:
341341
///
342342
/// let channel = socket.channel("topic")
343-
/// let ref1 = channel.on("event") { [weak self] (message) in
343+
/// let ref1 = channel.on(.all) { [weak self] (message) in
344344
/// self?.print("do stuff")
345345
/// }
346-
/// let ref2 = channel.on("event") { [weak self] (message) in
346+
/// let ref2 = channel.on(.all) { [weak self] (message) in
347347
/// self?.print("do other stuff")
348348
/// }
349-
/// channel.off("event", ref1)
349+
/// channel.off(.all, ref1)
350350
///
351351
/// Since unsubscription of ref1, "do stuff" won't print, but "do other
352352
/// stuff" will keep on printing on the "event"
@@ -355,7 +355,7 @@ public class Channel {
355355
/// - parameter callback: Called with the event's message
356356
/// - return: Ref counter of the subscription. See `func off()`
357357
@discardableResult
358-
public func on(_ event: String, callback: @escaping ((Message) -> Void)) -> Int {
358+
public func on(_ event: ChannelEvent, callback: @escaping ((Message) -> Void)) -> Int {
359359
var delegated = Delegated<Message, Void>()
360360
delegated.manuallyDelegate(with: callback)
361361

@@ -371,23 +371,23 @@ public class Channel {
371371
/// Example:
372372
///
373373
/// let channel = socket.channel("topic")
374-
/// let ref1 = channel.delegateOn("event", to: self) { (self, message) in
374+
/// let ref1 = channel.delegateOn(.all, to: self) { (self, message) in
375375
/// self?.print("do stuff")
376376
/// }
377-
/// let ref2 = channel.delegateOn("event", to: self) { (self, message) in
377+
/// let ref2 = channel.delegateOn(.all, to: self) { (self, message) in
378378
/// self?.print("do other stuff")
379379
/// }
380-
/// channel.off("event", ref1)
380+
/// channel.off(.all, ref1)
381381
///
382382
/// Since unsubscription of ref1, "do stuff" won't print, but "do other
383-
/// stuff" will keep on printing on the "event"
383+
/// stuff" will keep on printing on all "event" (*).
384384
///
385385
/// - parameter event: Event to receive
386386
/// - parameter owner: Class registering the callback. Usually `self`
387387
/// - parameter callback: Called with the event's message
388388
/// - return: Ref counter of the subscription. See `func off()`
389389
@discardableResult
390-
public func delegateOn<Target: AnyObject>(_ event: String,
390+
public func delegateOn<Target: AnyObject>(_ event: ChannelEvent,
391391
to owner: Target,
392392
callback: @escaping ((Target, Message) -> Void)) -> Int
393393
{
@@ -399,7 +399,7 @@ public class Channel {
399399

400400
/// Shared method between `on` and `manualOn`
401401
@discardableResult
402-
private func on(_ event: String, delegated: Delegated<Message, Void>) -> Int {
402+
private func on(_ event: ChannelEvent, delegated: Delegated<Message, Void>) -> Int {
403403
let ref = bindingRef
404404
bindingRef = ref + 1
405405

@@ -414,19 +414,19 @@ public class Channel {
414414
/// Example:
415415
///
416416
/// let channel = socket.channel("topic")
417-
/// let ref1 = channel.on("event") { _ in print("ref1 event" }
418-
/// let ref2 = channel.on("event") { _ in print("ref2 event" }
419-
/// let ref3 = channel.on("other_event") { _ in print("ref3 other" }
420-
/// let ref4 = channel.on("other_event") { _ in print("ref4 other" }
421-
/// channel.off("event", ref1)
422-
/// channel.off("other_event")
417+
/// let ref1 = channel.on(.insert) { _ in print("ref1 event" }
418+
/// let ref2 = channel.on(.insert) { _ in print("ref2 event" }
419+
/// let ref3 = channel.on(.update) { _ in print("ref3 other" }
420+
/// let ref4 = channel.on(.update) { _ in print("ref4 other" }
421+
/// channel.off(.insert, ref1)
422+
/// channel.off(.update)
423423
///
424424
/// After this, only "ref2 event" will be printed if the channel receives
425-
/// "event" and nothing is printed if the channel receives "other_event".
425+
/// "insert" and nothing is printed if the channel receives "update".
426426
///
427427
/// - parameter event: Event to unsubscribe from
428428
/// - paramter ref: Ref counter returned when subscribing. Can be omitted
429-
public func off(_ event: String, ref: Int? = nil) {
429+
public func off(_ event: ChannelEvent, ref: Int? = nil) {
430430
bindingsDel.removeAll { (bind) -> Bool in
431431
bind.event == event && (ref == nil || ref == bind.ref)
432432
}
@@ -437,14 +437,14 @@ public class Channel {
437437
/// Example:
438438
///
439439
/// channel
440-
/// .push("event", payload: ["message": "hello")
440+
/// .push(.update, payload: ["message": "hello")
441441
/// .receive("ok") { _ in { print("message sent") }
442442
///
443443
/// - parameter event: Event to push
444444
/// - parameter payload: Payload to push
445445
/// - parameter timeout: Optional timeout
446446
@discardableResult
447-
public func push(_ event: String,
447+
public func push(_ event: ChannelEvent,
448448
payload: [String: Any],
449449
timeout: TimeInterval = Defaults.timeoutInterval) -> Push
450450
{
@@ -476,7 +476,7 @@ public class Channel {
476476
///
477477
/// Example:
478478
////
479-
/// channel.leave().receive("ok") { _ in { print("left") }
479+
/// channel.unsubscribe().receive("ok") { _ in { print("left") }
480480
///
481481
/// - parameter timeout: Optional timeout
482482
/// - return: Push that can add receive hooks
@@ -578,13 +578,13 @@ public class Channel {
578578
}
579579

580580
/// Triggers an event to the correct event bindings created by
581-
//// `channel.on("event")`.
581+
//// `channel.on(event)`.
582582
///
583583
/// - parameter event: Event to trigger
584584
/// - parameter payload: Payload of the event
585585
/// - parameter ref: Ref of the event. Defaults to empty
586586
/// - parameter joinRef: Ref of the join event. Defaults to nil
587-
func trigger(event: String,
587+
func trigger(event: ChannelEvent,
588588
payload: [String: Any] = [:],
589589
ref: String = "",
590590
joinRef: String? = nil)
@@ -597,12 +597,6 @@ public class Channel {
597597
trigger(message)
598598
}
599599

600-
/// - parameter ref: The ref of the event push
601-
/// - return: The event name of the reply
602-
func replyEventName(_ ref: String) -> String {
603-
return "chan_reply_\(ref)"
604-
}
605-
606600
/// The Ref send during the join message.
607601
var joinRef: String? {
608602
return joinPush.ref

0 commit comments

Comments
 (0)