{"id":19106550,"url":"https://github.com/scaledrone/scaledrone-swift","last_synced_at":"2025-04-30T18:04:07.507Z","repository":{"id":54499784,"uuid":"100286028","full_name":"ScaleDrone/Scaledrone-Swift","owner":"ScaleDrone","description":"Swift Client for Scaledrone Realtime Messaging Service (WIP)","archived":false,"fork":false,"pushed_at":"2021-02-15T10:20:16.000Z","size":142,"stargazers_count":26,"open_issues_count":2,"forks_count":6,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-07T22:59:27.967Z","etag":null,"topics":["real-time","scaledrone","swift","websocket","websocket-client"],"latest_commit_sha":null,"homepage":"https://www.scaledrone.com/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ScaleDrone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-14T16:08:10.000Z","updated_at":"2024-10-11T11:12:40.000Z","dependencies_parsed_at":"2022-08-13T17:50:53.106Z","dependency_job_id":null,"html_url":"https://github.com/ScaleDrone/Scaledrone-Swift","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScaleDrone%2FScaledrone-Swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScaleDrone%2FScaledrone-Swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScaleDrone%2FScaledrone-Swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScaleDrone%2FScaledrone-Swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ScaleDrone","download_url":"https://codeload.github.com/ScaleDrone/Scaledrone-Swift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783200,"owners_count":17201914,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["real-time","scaledrone","swift","websocket","websocket-client"],"created_at":"2024-11-09T04:08:37.905Z","updated_at":"2024-11-09T04:08:38.403Z","avatar_url":"https://github.com/ScaleDrone.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [Scaledrone](https://www.scaledrone.com/) Swift\n\n\u003e Use the Scaledrone Swift client to connect to the Scaledrone realtime messaging service\n\nThis project is still a work in progress, pull requests and issues are very welcome.\n\n\n## Installation\n\n### CocoaPods\n\nCheck out [Get Started](http://cocoapods.org/) tab on [cocoapods.org](http://cocoapods.org/).\n\nTo use Scaledrone in your project add the following 'Podfile' to your project\n\n```ruby\npod 'Scaledrone', '~\u003e 0.5.2'\n```\n\nThen run:\n```\npod install\n```\n\n### Carthage\n\nCheck out [the Carthage Quick Start instructions](https://github.com/Carthage/Carthage#quick-start).\n\nTo use Scaledrone with Carthage, add the following to your Cartfile:\n\n```ruby\ngithub \"ScaleDrone/Scaledrone-Swift\"\n```\n\nThen run:\n\n```\ncarthage update\n```\n\nAfter that, follow the [instructions on Carthage's docs](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application).\n\n### Swift Package Manager\n\n[Use Xcode to add this repo as a package.](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) Search for `https://github.com/ScaleDrone/Scaledrone-Swift`.\n\n## Usage\n\nFirst thing is to import the framework. See the Installation instructions on how to add the framework to your project.\n\n```swift\nimport Scaledrone\n```\n\nOnce imported, you can connect to Scaledrone.\n\n```swift\nscaledrone = Scaledrone(channelID: \"your-channel-id\")\nscaledrone.delegate = self\nscaledrone.connect()\n```\n\nAfter you are connected, there are some delegate methods that we need to implement.\n\n#### scaledroneDidConnect\n\n```swift\nfunc scaledroneDidConnect(scaledrone: Scaledrone, error: Error?) {\n    print(\"Connected to Scaledrone\")\n}\n```\n\n#### scaledroneDidReceiveError\n\n```swift\nfunc scaledroneDidReceiveError(scaledrone: Scaledrone, error: Error?) {\n    print(\"Scaledrone error\", error ?? \"\")\n}\n```\n\n#### scaledroneDidDisconnect\n\n```swift\nfunc scaledroneDidDisconnect(scaledrone: Scaledrone, error: Error?) {\n    print(\"Scaledrone disconnected\", error ?? \"\")\n}\n```\n\n## Authentication\n\nImplement the **`ScaledroneAuthenticateDelegate`** protocol and set an additional delegate\n```swift\nscaledrone.authenticateDelegate = self\n```\n\nThen use the authenticate method to authenticate using a JWT\n\n```swift\nscaledrone.authenticate(jwt: \"jwt_string\")\n```\n\n#### scaledroneDidAuthenticate\n\n```swift\nfunc scaledroneDidAuthenticate(scaledrone: Scaledrone, error: Error?) {\n    print(\"Scaledrone authenticated\", error ?? \"\")\n}\n```\n\n## Sending messages\n\n```swift\nscaledrone.publish(message: \"Hello from Swift\", room: \"myroom\")\n// Or\nroom.publish(message: [\"foo\": \"bar\", \"1\": 2])\n```\n\n## Subscribing to messages\n\nSubscribe to a room and implement the **`ScaledroneRoomDelegate`** protocol, then set additional delegation\n\n```swift\nlet room = scaledrone.subscribe(roomName: \"myroom\")\nroom.delegate = self\n```\n\n#### scaledroneRoomDidConnect\n\n```swift\nfunc scaledroneRoomDidConnect(room: ScaledroneRoom, error: Error?) {\n    print(\"Scaledrone connected to room\", room.name, error ?? \"\")\n}\n```\n\n#### scaledroneRoomDidReceiveMessage\n\nThe `member` argument exists when the message was sent to an [observable room](#observable-rooms) using the socket API (not the REST API).\n\n```swift\nfunc scaledroneRoomDidReceiveMessage(room: ScaledroneRoom, message: ScaledroneMessage) {\n    if message.member != nil {\n        // This message was sent to an observable room\n        // This message was sent through the socket API, not the REST API\n        print(\"Received message from member:\", message.memberID as Any)\n    }\n    \n    let data = message.data\n    \n    if let messageData = data as? [String: Any] {\n        print(\"Received a dictionary:\", messageData)\n    }\n    if let messageData = data as? [Any] {\n        print(\"Received an array:\", messageData)\n    }\n    if let messageData = data as? String {\n        print(\"Received a string:\", messageData)\n    }\n}\n```\n\n## Observable rooms\n\nObservable rooms act like regular rooms but provide additional functionality for keeping track of connected members and linking messages to members.\n\n### Adding data to the member object\n\nObservable rooms allow adding custom data to a connected user. The data can be added in two ways:\n\n1. Passing the data object to a new instance of Scaledrone in your Swift code.\n```swift\nlet scaledrone = Scaledrone(channelID: \"\u003cchannel_id\u003e\", data: [\"name\": \"Swift\", \"color\": \"#ff0000\"])\n```\nThis data can later be accessed like so:\n```swift\nfunc scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {\n    print(\"member joined with clientData\", member.clientData)\n}\n```\n\n2. Adding the data to the JSON Web Token as the `data` clause during [authentication](https://www.scaledrone.com/docs/jwt-authentication). This method is safer as the user has no way of changing the data on the client side.\n```json\n{\n  \"client\": \"client_id_sent_from_javascript_client\",\n  \"channel\": \"channel_id\",\n  \"data\": {\n    \"name\": \"Swift\",\n    \"color\": \"#ff0000\"\n  },\n  \"permissions\": {\n    \"^main-room$\": {\n      \"publish\": false,\n      \"subscribe\": false\n    }\n  },\n  \"exp\": 1408639878000\n}\n```\nThis data can later be accessed like so:\n```swift\nfunc scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {\n    print(\"member joined with authData\", member.authData)\n}\n```\n\n### Receiving the observable events\n\nImplement the **`ScaledroneObservableRoomDelegate`** protocol, then set additional delegation.\n\n\u003e Observable room names need to be prefixed with *observable-*\n\n```swift\nlet room = scaledrone.subscribe(roomName: \"observable-room\")\nroom.delegate = self\nroom.observableDelegate = self\n```\n\n#### scaledroneObservableRoomDidConnect\n```swift\nfunc scaledroneObservableRoomDidConnect(room: ScaledroneRoom, members: [ScaledroneMember]) {\n    // The list will contain yourself\n    print(members.map { (m: ScaledroneMember) -\u003e String in\n        return m.id\n    })\n}\n```\n\n#### scaledroneObservableRoomMemberDidJoin\n```swift\nfunc scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {\n    print(\"member joined\", member, member.id)\n}\n```\n\n#### scaledroneObservableRoomMemberDidLeave\n```swift\nfunc scaledroneObservableRoomMemberDidLeave(room: ScaledroneRoom, member: ScaledroneMember) {\n    print(\"member left\", member, member.id)\n}\n```\n\n## Message History\n\nWhen creating a Scaledrone room you can supply the number of messages to recieve from that room's history. The messages will arrive, in reverse chronological order and one by one, in `scaledroneRoomDidReceiveMessage`, just like real-time messages.\n\nIn order to recieve message history messages, this feature needs to be enabled in the [Scaledrone dashboard](http://dashboard.scaledrone.com). You can learn more about Message History and its limitations in [Scaledrone docs](https://www.scaledrone.com/docs/message-history).\n\n```\nlet room = scaledrone.subscribe(roomName: \"chat-room\", messageHistory: 50)\n```\n\n\n## Basic Example\n```swift\nimport UIKit\n\nclass ViewController: UIViewController, ScaledroneDelegate, ScaledroneRoomDelegate {\n\n    let scaledrone = Scaledrone(channelID: \"your-channel-id\")\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        scaledrone.delegate = self\n        scaledrone.connect()\n    }\n\n    func scaledroneDidConnect(scaledrone: Scaledrone, error: Error?) {\n        print(\"Connected to Scaledrone channel\", scaledrone.clientID)\n        let room = scaledrone.subscribe(roomName: \"notifications\")\n        room.delegate = self\n    }\n\n    func scaledroneDidReceiveError(scaledrone: Scaledrone, error: Error?) {\n        print(\"Scaledrone error\")\n    }\n\n    func scaledroneDidDisconnect(scaledrone: Scaledrone, error: Error?) {\n        print(\"Scaledrone disconnected\")\n    }\n\n    func scaledroneRoomDidConnect(room: ScaledroneRoom, error: Error?) {\n        print(\"Scaledrone connected to room\", room.name)\n    }\n\n    func scaledroneRoomDidReceiveMessage(room: ScaledroneRoom, message: String) {\n        print(\"Room received message:\", message)\n    }\n}\n```\n\nFor a longer example see the `ViewController.swift` file.\n\n## Migration notes\n\n### 0.5.0\n\nScaledrone 0.5.0 removes the use of `NSError` in favor of `Error` in the delegate methods, and adds support for Swift 5.\n\n### 0.5.2:\n\n`scaledroneRoomDidReceiveMessage(room:message:member)` was renamed to `scaledroneRoomDidReceiveMessage(room:message:)` and `message` is now of type `ScaledroneMessage` which includes the member and message IDs, the message's time as well as the data that was sent.\n\n## Todo:\n\n* Automatic reconnection\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscaledrone%2Fscaledrone-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscaledrone%2Fscaledrone-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscaledrone%2Fscaledrone-swift/lists"}