skip to Main Content

I am using v4 of socket-io nodejs server. I am using v16 of swift socket-io client.

static let manager = SocketManager(socketURL: URL(string: baseUrl)!, config: [.log(true), .compress])
 let socket: SocketIOClient? = manager.defaultSocket

Then in onAppear Property of SwiftUI view I have written following code

socket?.connect()
            
            
            socket?.on(clientEvent: .connect) {data, ack in
                print("socket connected")
            }
            
            socket?.on("userjoined", callback: { (data, ack) in
                debugPrint("here")
                debugPrint("(data)")
                statusText = data[0] as? String ?? ""
            })
            
            socket?.on("receiveMessage", callback: { (data, ack) in
                debugPrint("here12")
                debugPrint("(data)")
                let str = data[0] as! Dictionary<String, Any>
                viewModel.msgsList.append("(String(describing: str["title"])) (String(describing: str["email"]))")
            })
            
            socket?.on("disconnected", callback: { (data, ack) in
                debugPrint("here34")
                debugPrint("(data)")
            })

I get following logs

022-05-03 15:41:56.587211+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2]
2022-05-03 15:41:56.681857+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Joining namespace /
2022-05-03 15:41:56.684175+0530 ws-ios[18838:338830] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2022-05-03 15:41:56.684517+0530 ws-ios[18838:338830] LOG SocketManager: Adding engine
2022-05-03 15:41:56.698649+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Adding handler for event: connect
2022-05-03 15:41:56.698744+0530 ws-ios[18838:339266] LOG SocketEngine: Starting engine. Server: https://MY_URL.com
2022-05-03 15:41:56.698994+0530 ws-ios[18838:339266] LOG SocketEngine: Handshaking
2022-05-03 15:41:56.699101+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Adding handler for event: userjoined
2022-05-03 15:41:56.699300+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Adding handler for event: receiveMessage
2022-05-03 15:41:56.699506+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Adding handler for event: disconnected
2022-05-03 15:41:56.706344+0530 ws-ios[18838:339266] LOG SocketEnginePolling: Doing polling GET https://MY_URL.com/socket.io/?transport=polling&b64=1&EIO=4
2022-05-03 15:41:58.570648+0530 ws-ios[18838:339267] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
2022-05-03 15:41:58.622922+0530 ws-ios[18838:339267] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
2022-05-03 15:41:58.933217+0530 ws-ios[18838:339267] LOG SocketEnginePolling: Got polling response
2022-05-03 15:41:58.934444+0530 ws-ios[18838:339267] LOG SocketEnginePolling: Got poll message: 0{"sid":"JD6xrLvEJvsDy233AAAO","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":20000,"maxPayload":1000000}
2022-05-03 15:41:58.937097+0530 ws-ios[18838:339267] LOG SocketEngine: Got message: 0{"sid":"JD6xrLvEJvsDy233AAAO","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":20000,"maxPayload":1000000}
2022-05-03 15:41:58.959116+0530 ws-ios[18838:338830] LOG SocketManager: Engine opened Connect
2022-05-03 15:41:58.959234+0530 ws-ios[18838:339267] LOG SocketEnginePolling: Doing polling GET https://MY_URL.com/socket.io/?transport=polling&b64=1&EIO=4&sid=JD6xrLvEJvsDy233AAAO
2022-05-03 15:41:58.960781+0530 ws-ios[18838:339267] LOG SocketEngine: Writing poll: 0/, has data: false
2022-05-03 15:41:58.961072+0530 ws-ios[18838:339267] LOG SocketEnginePolling: Sending poll: 0/, as type: 4
2022-05-03 15:41:58.963670+0530 ws-ios[18838:339267] LOG SocketEnginePolling: Created POST string: 40/,
2022-05-03 15:41:58.966090+0530 ws-ios[18838:339267] LOG SocketEnginePolling: POSTing
2022-05-03 15:41:58.966287+0530 ws-ios[18838:339267] LOG SocketEnginePolling: Doing polling POST https://MY_URL.com/socket.io/?transport=polling&b64=1&EIO=4&sid=JD6xrLvEJvsDy233AAAO
2022-05-03 15:41:59.897212+0530 ws-ios[18838:339277] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
2022-05-03 15:41:59.902511+0530 ws-ios[18838:339277] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
2022-05-03 15:42:00.362048+0530 ws-ios[18838:339283] LOG SocketEnginePolling: Got polling response
2022-05-03 15:42:00.362379+0530 ws-ios[18838:339283] LOG SocketEnginePolling: Got poll message: 40{"sid":"m_m3eV5SRygPv4inAAAP"}
2022-05-03 15:42:00.362691+0530 ws-ios[18838:339283] LOG SocketEngine: Got message: 40{"sid":"m_m3eV5SRygPv4inAAAP"}
2022-05-03 15:42:00.363351+0530 ws-ios[18838:339283] LOG SocketEnginePolling: Doing polling GET https://MY_URL.com/socket.io/?transport=polling&b64=1&EIO=4&sid=JD6xrLvEJvsDy233AAAO
2022-05-03 15:42:00.363967+0530 ws-ios[18838:338830] LOG SocketParser: Parsing 0{"sid":"m_m3eV5SRygPv4inAAAP"}
2022-05-03 15:42:00.370844+0530 ws-ios[18838:338830] LOG SocketParser: Decoded packet as: SocketPacket {type: 0; data: [{
    sid = "m_m3eV5SRygPv4inAAAP";
}]; id: -1; placeholders: -1; nsp: /}
2022-05-03 15:42:00.371973+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Socket connected
2022-05-03 15:42:00.372224+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3]
2022-05-03 15:42:00.378048+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Handling event: connect with data: ["/", ["sid": m_m3eV5SRygPv4inAAAP]]
2022-05-03 15:42:00.378370+0530 ws-ios[18838:339283] LOG SocketEngineWebSocket: Sending ws: probe as type: 2
socket connected
2022-05-03 15:42:00.381198+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["Sec-WebSocket-Accept": "hO9PSta7ypMVlBDHEuYM6iZRCD4=", "Connection": "Upgrade", "Upgrade": "websocket", "Via": "1.1 vegur"]]
2022-05-03 15:42:00.618775+0530 ws-ios[18838:339266] LOG SocketEngine: Got message: 3probe
2022-05-03 15:42:00.619336+0530 ws-ios[18838:339266] LOG SocketEngine: Received probe response, should upgrade to WebSockets
2022-05-03 15:42:00.619731+0530 ws-ios[18838:339266] LOG SocketEngine: Upgrading transport to WebSockets
2022-05-03 15:42:00.620094+0530 ws-ios[18838:339266] LOG SocketEnginePolling: Sending poll:  as type: 6
2022-05-03 15:42:00.620489+0530 ws-ios[18838:339266] LOG SocketEnginePolling: Created POST string: 6
2022-05-03 15:42:00.621049+0530 ws-ios[18838:339266] LOG SocketEnginePolling: POSTing
2022-05-03 15:42:00.621621+0530 ws-ios[18838:338830] LOG SocketIOClient{/}: Handling event: pong with data: []
2022-05-03 15:42:00.723890+0530 ws-ios[18838:339277] LOG SocketEnginePolling: Got polling response
2022-05-03 15:42:00.724828+0530 ws-ios[18838:339277] LOG SocketEnginePolling: Got poll message: 6
2022-05-03 15:42:00.725858+0530 ws-ios[18838:339277] LOG SocketEngine: Got message: 6
2022-05-03 15:42:00.726999+0530 ws-ios[18838:339277] LOG SocketEngine: Switching to WebSockets
2022-05-03 15:42:00.727463+0530 ws-ios[18838:339277] LOG SocketEngineWebSocket: Sending ws:  as type: 5
2022-05-03 15:42:00.727999+0530 ws-ios[18838:339277] LOG SocketEngine: Flushing probe wait

There are definitely no issues from server side as our web app which uses reactjs and socket-io client library for web is able to connect to the server and pass messages.
I am not able to figure out what exactly is going wrong.

2

Answers


  1. I stumbled upon this problem recently. I was using socket.io ^4.4.1 on the server. But there was no mention of how to connect with the v4 server in the documentation, and the compatibility page was not loading, so I dig further.

    I found out that "socket.io-client-swift" release version is 15, while 16 is probably still in development.

    There is this compatibility table source code in which, sections 1 and 2 are required for version 15. I needed to add both the server and the client config for it to work.

    Login or Signup to reply.
  2. if your baseURL contains an namespace, then it is the namespace issue, you have to put it into socket namespace like below:

    let socket = socketManager.socket(forNamespace: "/yourUrlNamespace")
    

    They didn’t mention the namespace and domain criteria on socket.io-client-swift documentation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search