{"id":15038831,"url":"https://github.com/sacoo7/socketcluster-client-swift","last_synced_at":"2025-10-08T22:22:27.035Z","repository":{"id":28580905,"uuid":"104928327","full_name":"sacOO7/socketcluster-client-swift","owner":"sacOO7","description":"Native iOS/macOS client written in swift 4/swift 5","archived":false,"fork":false,"pushed_at":"2022-12-12T23:46:08.000Z","size":77,"stargazers_count":22,"open_issues_count":23,"forks_count":38,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-09-29T16:32:54.091Z","etag":null,"topics":["client-library","ios-swift","macos","socketcluster","socketcluster-client","swift4"],"latest_commit_sha":null,"homepage":"https://socketcluster.io/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sacOO7.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-09-26T19:23:48.000Z","updated_at":"2025-07-23T21:48:41.000Z","dependencies_parsed_at":"2022-08-07T14:00:15.053Z","dependency_job_id":null,"html_url":"https://github.com/sacOO7/socketcluster-client-swift","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/sacOO7/socketcluster-client-swift","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sacOO7","download_url":"https://codeload.github.com/sacOO7/socketcluster-client-swift/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sacOO7%2Fsocketcluster-client-swift/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000782,"owners_count":26082851,"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","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["client-library","ios-swift","macos","socketcluster","socketcluster-client","swift4"],"created_at":"2024-09-24T20:40:23.617Z","updated_at":"2025-10-08T22:22:27.016Z","avatar_url":"https://github.com/sacOO7.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# socketcluster-client-swift\nNative iOS/macOS client written in swift\n    \nOverview\n--------\nThis client provides following functionality\n\n- Easy to setup and use\n- Support for emitting and listening to remote events\n- Pub/sub\n- Authentication (JWT)\n\nClient supports following platforms\n\n- iOS \u003e= 8.0\n- macOS\u003e= 10.10\n- watchOS \u003e= 2.0\n- tvOS \u003e= 9.0 \n\nInstallation and Use\n--------------------\n\n### [CocoaPods](http://cocoapods.org)\n\n```ruby\npod 'ScClient'\n```\n\n### Swift Package Manager\n\n- To install add this to depedencies section of Package.swift\n\n ```swift\n     dependencies: [\n     \t// other dependencies \n    \t.package(url: \"https://github.com/sacOO7/ScClient\", from: \"2.0.1\")\n\t]\n ```\n- To use the library add this to target dependencies\n\n ```swift\n    targets: [\n        .target(\n            name: \"tool\",\n            dependencies: [\n                \"ScClient\"\n            ])\n    ]\n ```\n\nDescription\n-----------\nCreate instance of `scclient` by passing url of socketcluster-server end-point \n\n```swift\n    //Create a client instance\n    var client = ScClient(url: \"http://localhost:8000/socketcluster/\")\n    \n```\n**Important Note** : Default url to socketcluster end-point is always *ws://somedomainname.com/socketcluster/*.\n\n#### Registering basic listeners\n \n- Different closure functions are given as an argument to register listeners\n- Example : main.swift\n\n```swift\n        import Foundation\n        import ScClient\n        \n        var client = ScClient(url: \"http://localhost:8000/socketcluster/\")\n\n        var onConnect = {\n            (client :ScClient) in\n            print(\"Connnected to server\")\n        }\n\n        var onDisconnect = {\n            (client :ScClient, error : Error?) in\n                print(\"Disconnected from server due to \", error?.localizedDescription)\n        }\n\n        var onAuthentication = {\n            (client :ScClient, isAuthenticated : Bool?) in\n            print(\"Authenticated is \", isAuthenticated)\n            startCode(client : client)\n        }\n\n        var onSetAuthentication = {\n            (client : ScClient, token : String?) in\n            print(\"Token is \", token)\n        }\n        client.setBasicListener(onConnect: onConnect, onConnectError: nil, onDisconnect: onDisconnect)\n        client.setAuthenticationListener(onSetAuthentication: onSetAuthentication, onAuthentication: onAuthentication)\n        \n        client.connect()\n        \n        while(true) {\n            RunLoop.current.run(until: Date())\n            usleep(10)\n        }\n        \n        func startCode(client scclient.Client) {\n        \t// start writing your code from here\n        \t// All emit, receive and publish events\n        }\n        \n```\n\n\n#### Connecting to server\n\n- For connecting to server:\n\n```swift \n    //This will send websocket handshake request to socketcluster-server\n    client.connect()\n```\n\n#### Getting connection status\n\n```swift \n    //This will send websocket handshake request to socketcluster-server\n    var status = client.isConnected()\n```\n\n\nEmitting and listening to events\n--------------------------------\n#### Event emitter\n\n- eventname is name of event and message can be String, boolean, Int or Object\n\n```swift\n\n    client.emit(eventName: eventname, data: message as AnyObject)\n    \n  //client.emit(eventName: \"chat\", data: \"This is my sample message\" as AnyObject)\n  \n```\n\n- To send event with acknowledgement\n\n```swift\n\n    client.emitAck(eventName: \"chat\", data: \"This is my sample message\" as AnyObject, ack : {\n    \t    (eventName : String, error : AnyObject? , data : AnyObject?) in\n            print(\"Got data for eventName \", eventName, \" error is \", error, \" data is \", data)  \n    })\n\t\n```\n\n#### Event Listener\n\n- For listening to events :\n\nThe object received can be String, Boolean, Int or Object\n\n```swift\n    // Receiver code without sending acknowledgement back\n    client.on(eventName: \"yell\", ack: {\n    \t    (eventName : String, data : AnyObject?) in\n            print(\"Got data for eventName \", eventName, \" data is \", data)\n    })\n    \n```\n\n- To send acknowledgement back to server\n\n```swift\n    // Receiver code with ack\n    client.onAck(eventName: \"yell\", ack: {\n            (eventName : String, data : AnyObject?, ack : (AnyObject?, AnyObject?) -\u003e Void) in\n            print(\"Got data for eventName \", eventName, \" data is \", data)\n            ack(\"This is error \" as AnyObject, \"This is data \" as AnyObject)\n    })\n        \n```\n\n\n\nImplementing Pub-Sub via channels\n---------------------------------\n\n#### Creating channel\n\n- For creating and subscribing to channels:\n\n```swift \n    // without acknowledgement\n    client.subscribe(channelName: \"yell\")\n    \n    //with acknowledgement\n    client.subscribeAck(channelName: \"yell\", ack : {\n        (channelName : String, error : AnyObject?, data : AnyObject?) in\n        if (error is NSNull) {\n            print(\"Successfully subscribed to channel \", channelName)\n        } else {\n            print(\"Got error while subscribing \", error)\n        }\n    })\n```\n\n\n#### Publishing event on channel\n\n- For publishing event :\n\n```swift\n\n\t// without acknowledgement\n\tclient.publish(channelName: \"yell\", data: \"I am sending data to yell\" as AnyObject)\n\n\n\t// with acknowledgement\n\tclient.publishAck(channelName: \"yell\", data: \"I am sending data to yell\" as AnyObject, ack : {\n\t\t(channelName : String, error : AnyObject?, data : AnyObject?) in\n\t\tif (error is NSNull) {\n\t\t     print(\"Successfully published to channel \", channelName)\n\t\t}else {\n\t\t     print(\"Got error while publishing \", error)\n\t\t}\n\t})\n``` \n \n#### Listening to channel\n\n- For listening to channel event :\n\n```swift\n        client.onChannel(channelName: \"yell\", ack: {\n    \t\t(channelName : String , data : AnyObject?) in\n    \t\tprint (\"Got data for channel\", channelName, \" object data is \", data)\n\t})\n``` \n     \n#### Un-subscribing to channel\n\n```swift\n    // without acknowledgement\n    client.unsubscribe(channelName: \"yell\")\n    \n    //with acknowledgement\n    client.unsubscribeAck(channelName: \"yell\", ack : {\n        (channelName : String, error : AnyObject?, data : AnyObject?) in\n        if (error is NSNull) {\n            print(\"Successfully unsubscribed to channel \", channelName)\n        } else {\n            print(\"Got error while unsubscribing \", error)\n        }\n    })\n```\n\n#### Disable SSL Certificate Verification\n\n```swift\n\tvar client = ScClient(url: \"http://localhost:8000/socketcluster/\")\n        client.disableSSLVerification(true)\n```\n### Custom Queue\n\nA custom queue can be specified when delegate methods are called. By default `DispatchQueue.main` is used, thus making all delegate methods calls run on the main thread. It is important to note that all WebSocket processing is done on a background thread, only the delegate method calls are changed when modifying the queue. The actual processing is always on a background thread and will not pause your app.\n\n```swift\n\tvar client = ScClient(url: \"http://localhost:8000/socketcluster/\")\n\t//create a custom queue\n\tclient.setBackgroundQueue(queueName : \"com.example.chatapp\")\n```\n\n### Custom Headers\n\nYou can also override the default websocket headers with your own custom ones like so:\n\n```swift\n\tvar request = URLRequest(url: URL(string: \"http://localhost:8000/socketcluster/\")!)\n\trequest.timeoutInterval = 5\n\trequest.setValue(\"someother protocols\", forHTTPHeaderField: \"Sec-WebSocket-Protocol\")\n\trequest.setValue(\"14\", forHTTPHeaderField: \"Sec-WebSocket-Version\")\n\trequest.setValue(\"Everything is Awesome!\", forHTTPHeaderField: \"My-Awesome-Header\")\n\tvar client = ScClient(URLRequest: request)\n```\n\n### Custom HTTP Method\n\nYour server may use a different HTTP method when connecting to the websocket:\n\n```swift\n\tvar request = URLRequest(url: URL(string: \"http://localhost:8000/socketcluster/\")!)\n\trequest.httpMethod = \"POST\"\n\trequest.timeoutInterval = 5\n\tvar client = ScClient(URLRequest: request)\n```\n\n### Protocols\n\nIf you need to specify a protocol, simple add it to the init:\n\n```swift\n\t//chat and superchat are the example protocols here\n\tvar request = URLRequest(url: URL(string: \"http://localhost:8000/socketcluster/\")!)\n\tvar client = ScClient(URLRequest: request, protocols: [\"chat\",\"superchat\"])\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsacoo7%2Fsocketcluster-client-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsacoo7%2Fsocketcluster-client-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsacoo7%2Fsocketcluster-client-swift/lists"}