Socket.io-client-swift: Can't connect to CryptoCompare web socket

Created on 2 Jul 2017  ·  16Comments  ·  Source: socketio/socket.io-client-swift

I'm probably missing something obvious. The url is wss://streamer.cryptocompare.com, as can be found on their website. I can connect to it fine using this online socket.io client, but I can't get it to work using this library:

let url = URL(string: "wss://streamer.cryptocompare.com")!
let socket = SocketIOClient(socketURL: url, config: [.log(true)])
socket.connect()

The logs I'm getting are

LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus]
LOG SocketIOClient: Adding engine
LOG SocketIOClient: Client is being released
LOG SocketEngine: Starting engine. Server: wss://streamer.cryptocompare.com
LOG SocketEngine: Handshaking
LOG SocketEnginePolling: Doing polling GET http://streamer.cryptocompare.com/socket.io/?transport=polling&b64=1
TIC TCP Conn Failed [1:0x61800017e600]: 1:60 Err(60)
HTTP load failed (error code: -1001 [1:60]) for Task <21F81135-348E-4508-95B3-419E2302AF3B>.<1>
Task <21F81135-348E-4508-95B3-419E2302AF3B>.<1> finished with error - code: -1001
ERROR SocketEnginePolling: The request timed out.
ERROR SocketEngine: The request timed out.
LOG SocketEngine: Engine is being released
question

Most helpful comment

Thanks @pandeyshivang, works great!

All 16 comments

This library isn't a WebSocket client. Check out https://github.com/daltoniam/Starscream (It's what this library uses for WebSocket.)

@timvermeulen Hey Tim, did you manage to connect in the end? I'm having issues connecting to the same websocket but I'm trying in Python. Just wanted to follow up on this as you may have some useful info for me. Thanks!

@yudevit I haven't attempted it since, sorry about that 😕

Hey @timvermeulen
replace URL(string: "wss://streamer.cryptocompare.com")!
with
URL(string: "https://streamer.cryptocompare.com/")! and it will connect without any issue.

But I am stuck at

self.socket?.emitWithAck("SubAdd", [ "subs" : ["0~CCCAGG~BTC~USD"]]).timingOut(after: 0, callback: { (data) in
print("data is : (data)")
})

callback never called :(

yeah finally @timvermeulen @nuclearace @yudevit @IdeaCentricity @banjun ,

import SocketIO
class SocketIOManager :NSObject{
    var data:NSString?
    static let sharedInstant = SocketIOManager()
    var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "https://streamer.cryptocompare.com")! as URL, config: [.log(true), .forcePolling(true)])

    override init() {
        super.init()
        print("init called")
        let dict = [ "subs" : ["5~CCCAGG~BTC~USD"]]


        socket.on(clientEvent: .connect) {data, ack in
            print("socket connected \(data) \(ack)")
            self.socket.emit("SubAdd", dict)
        }


        socket.on("m", callback: {data,ack in
            print("M printed")
            print("M \(data)")
            let myString: String = String(describing: data[0])
            if myString == "3~LOADCOMPLETE"{
                 self.socket.emit("SubRemove", dict)
               // SocketIOManager.init()
            }else {
                var myStringArr = myString.components(separatedBy: "~")
                print("Price Updated \(myStringArr[5])")

            }


        })
    }
    func establishConnection() {
        socket.connect()

    }
    func closeConnection() {
        socket.disconnect()
    }



}

Thanks @pandeyshivang, works great!

@pandeyshivang have you do same thing in android because in iOS your solution works but in android it is not working .

@imobdevtech follow this(Swift) implemention in JAVA and use https://github.com/socketio/socket.io-client-java

@pandeyshivang we already follow all the step which is in swift but we stuck at emit statement , everytime we got 1 - bad request . can you please share that portion code in android if feasible

@imobdevtech wait...i'll share code

@pandeyshivang finally find the solution thank you for your support

@imobdevtech i think you were missing or wrong emit before soket.onlistener callback.

@pandeyshivang finally find the solution thank you for your support

could you please post the correct solution you found. I'm having the same issue here.

Found the right way for Java version. The following should work:

JSONObject object = new JSONObject();
JSONArray arr = new JSONArray();
try {
arr.put("11~BTC'");
object.put("subs", arr);
socket.emit("SubAdd", object);
} catch (JSONException e) {
e.printStackTrace();
}

Found the right way for Java version. The following should work:

JSONObject object = new JSONObject();
JSONArray arr = new JSONArray();
try {
arr.put("11~BTC'");
object.put("subs", arr);
socket.emit("SubAdd", object);
} catch (JSONException e) {
e.printStackTrace();
}

please try to use hashmap and arraylist instead of jsonobject and array

@pandeyshivang The performance is the same as HashMap and no extra overhead.
Also, considering that we are dealing with actual JSON on the request, I highly recommend it.
The actual docs use JSONObject too.
https://github.com/socketio/socket.io-client-java - check the readme.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthiasp42 picture matthiasp42  ·  3Comments

Dohab picture Dohab  ·  4Comments

gaetanm picture gaetanm  ·  4Comments

roma86 picture roma86  ·  4Comments

huangxiaolin0425 picture huangxiaolin0425  ·  6Comments