appwrite / appwrite/sdk-for-apple

🚀 Feature: Binding realtime channels to an Enum

Open
#49 0 comments 2 reactions 1 assignee Assigned to @ChiragAgg5k View on GitHub
enhancement
Dominant language
Swift
Stars
123
Forks
37
Avg merge
2h 50m
Merged PRs (30d)
1

Description

### 🔖 Feature description

Currently to subscribe to a realtime channel we use string values documented in [appwrite docs](https://appwrite.io/docs/apis/realtime). The problem is we need to create channel strings everytime manually. Instead, we can use the power of Swift's Enums. I've already created and extension and I'm sharing it here. Unfortunately I have no time to send a PR for it right now. People can find and use it from here or maybe someone can open a PR on behalf of me.

### 🎤 Pitch

The Use Case: I'm using the example code shared in documentation here.

Currently we subscribe the channel like this; via manual typed strings:
```swift
import Appwrite
import AppwriteModels

let client = Client()
.setEndpoint("https://cloud.appwrite.io/v1")
.setProject("")

let realtime = Realtime(client)

realtime.subscribe(channels: ["databases.A.collections.A.documents.A", "files"]) { response in
// Callback will be executed on changes for documents A and all files.
print(String(describing: response))
}
```

With my extension we can subscribe to channels without knowing what the channel's string is.

```swift

import Appwrite
import AppwriteModels

let client = Client()
.setEndpoint("https://cloud.appwrite.io/v1")
.setProject("")

let realtime = Realtime(client)

// Here we use my Channel extension to auto create required channel string
let channelToDocumentA = Realtime.Channel.document(databaseID: "A", collectionID: "A", documentID: "A")
let channelToFiles = Realtime.Channel.files

realtime.subscribe(channels: [channelToDocumentA.toString(), channelToFiles.toString()]) { response in
// Callback will be executed on changes for documents A and all files.
print(String(describing: response))
}

```

And here is the extension of mine:

```swift
import Foundation
import Appwrite

extension Realtime {

/// All channels available you can subscribe to.
public enum Channel {

/// All account related events (session create, name update...)
case account

/// Any create/update/delete events to any document in a collection
case documentsInCollection(databaseID: String, collectionID: String)
/// Any create/update/delete events to any document
case documents
/// Any update/delete events to a given document
case document(databaseID: String, collectionID: String, documentID: String)

/// Any create/update/delete events to any file
case files
/// Any update/delete events to a given file of the given bucket
case file(bucketID: String, fileID: String)
/// Any update/delete events to any file of the given bucket
case filesInBucket(bucketID: String)

/// Any create/update/delete events to a any team
case teams
/// Any update/delete events to a given team
case team(teamID: String)

/// Any create/update/delete events to a any membership
case memberships
/// Any update/delete events to a given membership
case membership(membershipID: String)

/// Any update to executions
case executions
/// Any update to a given execution
case execution(executionID: String)

/// Any execution event to a given function
case funtion(functionID: String)

/// String representation of the channel used for subscribing
public func toString() -> String {
return switch self {
case .account:
"account"

case .documentsInCollection(let databaseID, let collectionID):
"databases.\(databaseID).collections.\(collectionID).documents"

case .documents:
"documents"

case .document(let databaseID, let collectionID, let documentId):
"databases.\(databaseID).collections.\(collectionID).documents.\(documentId)"

case .files:
"files"

case .file(let bucketID, let fileID):
"buckets.\(bucketID).files.\(fileID)"

case .filesInBucket(let bucketID):
"buckets.\(bucketID).files"

case .teams:
"teams"

case .team(let teamID):
"teams.\(teamID)"

case .memberships:
"memberships"

case .membership(let membershipID):
"memberships.\(membershipID)"

case .executions:
"executions"

case .execution(let executionID):
"executions.\(executionID)"

case .funtion(let functionID):
"functions.\(functionID)"
}
}
}
}

extension Realtime.Channel: Comparable, Equatable, Identifiable {
public typealias ID = String

public var id: ID {
self.toString()
}

public static func < (lhs: Realtime.Channel, rhs: Realtime.Channel) -> Bool {
lhs.toString() < rhs.toString()
}

public static func == (lhs: Realtime.Channel, rhs: Realtime.Channel) -> Bool {
lhs.toString() == rhs.toString()
}
}

```

### 👀 Have you spent some time to check if this issue has been raised before?

- [X] I checked and didn't find similar issue

### 🏢 Have you read the Code of Conduct?

- [X] I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.