Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,67 @@ allUserId99Changes.subscribe()
allUserId99Changes.unsubscribe()
allUserId99Changes.off(.all)
```
### Broadcast

* Listen for `broadcast` messages:

```swift
let channel = client.channel(.table("channel_id", schema: "someChannel"), options: .init(presenceKey: "user_uuid"))
channel.on(.broadcast) { message in
let payload = message.payload["payload"]
let event = message.payload["event"]
let type = message.payload["type"]
print(type, event, payload)
}

channel.join()
```

* Send `broadcast` messages:

```swift
let channel = client.channel(.table("channel_id", schema: "someChannel"), options: .init(presenceKey: "user_uuid"))
channel.join()

channel.broadcast(event: "my_event", payload: ["hello": "world"])
```
### Presence

Presence can be used to share state between clients.

* Listen to presence `sync` events to track state changes:

```swift
let channel = client.channel(.table("channel_id", schema: "someChannel"), options: .init(presenceKey: "user_uuid"))
let presence = Presence(channel: channel)

presence.onSync {
print("presence sync", presence?.state, presence?.list())
}

channel.join()
// ...
```

* Track presence state changes:

```swift
let channel = client.channel(.table("channel_id", schema: "someChannel"), options: .init(presenceKey: "user_uuid"))
channel.join()

channel.track(payload: [
["hello": "world]
])
```

* Remove tracked presence state changes:

```swift
let channel = client.channel(.table("channel_id", schema: "someChannel"), options: .init(presenceKey: "user_uuid"))
channel.join()

channel.untrack()
```
## Credits

- https://github.com/supabase/realtime-js
Expand Down
Loading