{"id":17506335,"url":"https://github.com/mobile-simformsolutions/socketio_starter","last_synced_at":"2025-04-23T12:27:17.213Z","repository":{"id":108787465,"uuid":"292179722","full_name":"mobile-simformsolutions/SocketIO_Starter","owner":"mobile-simformsolutions","description":"Socket.IO configuration demo using swift","archived":false,"fork":false,"pushed_at":"2020-09-02T05:17:37.000Z","size":53,"stargazers_count":17,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T23:41:10.577Z","etag":null,"topics":["ios","namespace","reconnects","socket-connection","socket-instance","socket-io-demo","socket-shared-class","socketio","subdomain","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mobile-simformsolutions.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-02T04:35:52.000Z","updated_at":"2023-07-27T16:27:29.000Z","dependencies_parsed_at":"2023-06-05T04:00:15.195Z","dependency_job_id":null,"html_url":"https://github.com/mobile-simformsolutions/SocketIO_Starter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobile-simformsolutions%2FSocketIO_Starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobile-simformsolutions%2FSocketIO_Starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobile-simformsolutions%2FSocketIO_Starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobile-simformsolutions%2FSocketIO_Starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mobile-simformsolutions","download_url":"https://codeload.github.com/mobile-simformsolutions/SocketIO_Starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250432955,"owners_count":21429781,"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":["ios","namespace","reconnects","socket-connection","socket-instance","socket-io-demo","socket-shared-class","socketio","subdomain","swift"],"created_at":"2024-10-20T03:37:21.254Z","updated_at":"2025-04-23T12:27:17.203Z","avatar_url":"https://github.com/mobile-simformsolutions.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SocketIO Starter\n\n  \n\n[![Swift 5.0](https://img.shields.io/badge/Swift-5.0-orange.svg?style=flat)](https://swift.org)\n\n  \n\nThis repository is an example project of [Socket.IO](https://github.com/socketio/socket.io-client-swift) configuration in IOS using Swift.\n\n  \n\n### Included Points\n\n- Socket.io shared class that helps to establish socket in project.\n\n- Socket configuration (establish, connect, disconnect, etc...)\n\n- Handle socket connection.\n\n\n  \n\n### SocketIo Basic Functions Description\n\n  \n\n##### 1. How to create Socket instance and connect to the end server?\n\n1. Initialize `SocketManager:`\n\n```\n\nlet manager = SocketManager(socketURL: URL(string: AppConstant.socketURL)!, config: [.log(false), .reconnects(true), .forcePolling(true)])\n\n```\n\n- In config parameter, client can add multiple properties with type SocketIOClientConfiguration.\n\n.log(Bool) -\u003e If passed `true`, the client will log debug information. This should be turned off in production code.\n\n.reconnects(Bool) -\u003e If passed `false`, the client will not reconnect when it loses connection. Useful if you want full control over when reconnects happen.\n\n.forcePolling(Bool) -\u003e If passed `true`, the only transport that will be used will be HTTP long-polling.\n\n  \n\n2. get socket instance from `SocketManager:`\n\n```\n\nmanager?.defaultSocket\n\n```\n\n- Client also can add `nsp` while fetching socket instance:\n\nnsp(nameSpace) -\u003e Use to add subDomain for the socket connection.\n\nFor example, for the server https://www.xyz.com has another subdomain \"/user_socket\" for socket connection.\n\nTo connect with the subDomain, create socket with nameSpace.\n\n```\n\nmanager?.socket(forNamespace: \"user_socket\")\n\n```\n\n3. Client can connect socket instance with `.connect()` default method.\n\n```\n\nsocket?.connect()\n\n```\n\n  \n\n##### 2. How to add Header in socket?\n\nClient can add header through `manager.config` variable with `.extraHeaders` param.\n\n```\n\nvar dictHeader = [String: String]()\n\ndictHeader[\"Authorization\"] = \"Bearer \u003c---Connection Token---\u003e\"\n\nmanager?.config = SocketIOClientConfiguration(arrayLiteral: .compress, .extraHeaders(dictHeader))\n\n```\n\n`Important:`\n\nClient can only add header before connection established. After socket connection, client can not change header object. For that, client need to create new instance of socket and reconnect to the server.\n\n  \n\n##### 3. How to emit event with socket?\n\n`.emit` method is use to pass the dictionary object to the server with eventName.\n\n```\n\nSocketIOManager.shared.socket?.emitWithAck(\"eventName\", reqObj.toDictionary()).timingOut(after: 20) { data in\n\n}\n\n```\n\n  \n\n##### 4. How to receive response from the server?\n\nCall this `.on` method to observe the events. client gets notified when response has come.\n\n```\n\nSocketIOManager.shared.socket?.on(\"eventName\") { data, ack in\n\nguard let dictResp = data[0] as? [String: Any] else {\n\nreturn\n\n}\n\n}\n\n```\n\nHere, response has come in first index of Array type in `.on` closure.\n\n  \n\n##### 5. How to disable socket listener?\n\nsocket.off will remove socket listener. Where id is unique UUID. Better to release listener before deinitialization.\n\nIt will help to release memory from ARC.\n\n```\n\nSocketIOManager.shared.socket?.off(id: listenerVariable)\n\n```\n\n  \n\n### Important Note\n\n- IOS Doesn't allow socket to connect in the background. For better usecase, disconnect socket when goes to background and connect it in foreground.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmobile-simformsolutions%2Fsocketio_starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmobile-simformsolutions%2Fsocketio_starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmobile-simformsolutions%2Fsocketio_starter/lists"}