Socket.io-client-swift: Socket emit not working.

Created on 11 Sep 2017  ·  10Comments  ·  Source: socketio/socket.io-client-swift

Here is my code.
override func viewDidLoad() {
super.viewDidLoad()

    // Do any additional setup after loading the view.
    socket.on("connect") {data, ack in
        print("my socket connected")

        var param = [String: Any]()
        param["client_id"] = 33

        self.socket.emit("client_connect", param)
        self.socket.emit("get_workers")
    }

    socket.on("get_all_workers") { (data, ack) in
        print("Get All Workers")
    }

    socket.on(clientEvent: .disconnect) { (data, ack) in
        print("Socket disconnected")
    }

    socket.connect()
}

"My Socket Connected" logged successfully but "Get All Workers" doesn't logged. Socket name and url are correct and working fine in android version. Can you plz help me what is wrong?

question

All 10 comments

I can't see anything wrong here. Try turning on logging and see if you notice anything

Well, turning on logging means?
let socket = SocketIOClient(socketURL: URL(string: "http://neeleez.com:8383/client")!, config: [.log(false), .compress])

I need to set the false as true?

This is my full code.
var socket = SocketIOClient(socketURL: URL(string: "http://206.225.95.239:8383/client")!)

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    socket.on("connect") {data, ack in
        print("my socket connected")

        // var param = [String: Any]()
        // param["client_id"] = 33

        self.socket.emit("client_connect", ["client_id": 33])
        self.socket.emit("get_workers")
    }

    socket.on("get_all_workers") { (data, ack) in
        print("Get All Workers")
    }

    socket.on(clientEvent: .disconnect) { (data, ack) in
        print("Socket disconnected")
    }

    socket.connect()
}

it logs "my socket connected" but "Get All Workers" doesn't logged. Any suggestion would be appreciate.

Yes it should be .log(true).

No error in my code? Actually server seems ok because android is working fine. I think this issue is coming from iOS client.
in server console, it doesn't logged me as connected but in iOS client side, "my socket connected" logged.
Any clue from this?

Maybe something in your self.socket.emit("client_connect", ["client_id": 33]) emit? If something in that does authentication such that an emit of get_workers won't do anything, maybe there's something wrong there.
Another thing is if your url is using a namespace, i.e http://someurl.com/mynamesapce In the JS and Android client, would be done with the .nsp(String) option in this client.

Ah yes, I see in your above comment you're doing something like that.

var socket = SocketIOClient(socketURL: URL(string: "http://206.225.95.239:8383/client")!)

should actually be

var socket = SocketIOClient(socketURL: URL(string: "http://206.225.95.239:8383")!, config: [.nsp("/client")])

Sorry I missed that before.

I should probably put something in the FAQ about that.

Yes it works. Thanks a lot.

could you tell me why we need to used nsp with socket URL? I have same issue my socket is working in android well but not working in ios.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

janiduw picture janiduw  ·  5Comments

leeyuno picture leeyuno  ·  5Comments

huangxiaolin0425 picture huangxiaolin0425  ·  6Comments

Dohab picture Dohab  ·  4Comments

Seonift picture Seonift  ·  4Comments