Skip to content

Update from upstream SwiftPhoenixClient #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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