How do I connect to my WebSocket server?

This library is NOT a WebSockets library. This library is only for servers that implement the socket.io protocol, such as socket.io. If you need a plain WebSockets client check out Starscream for Swift and JetFire for Objective-C.

Why isn’t my event handler being called?

One of the most common reasons your event might not be called is if the client is released by ARC.

Take this code for example:

class SocketManager {
    func addHandlers() {
        let socket = SocketIOClient(socketURL: URL(string: "http://somesocketioserver.com")!)

        socket.on("myEvent") {data, ack in
            print(data)
        }
    }

}

This code is incorrect, and the event handler will never be called. Because as soon as this method is called socket will be released and its memory reclaimed.

A correct way would be:

class SocketManager {
    let socket = SocketIOClient(socketURL: URL(string: "http://somesocketioserver.com")!)

    func addHandlers() {
        socket.on("myEvent") {data, ack in
            print(data)
        }
    }
}


Another case where this might happen is if you use namespaces in your socket.io application.

In the JavaScript client a url that looks like http://somesocketioserver.com/client would be done with the nsp config.

let socket = SocketIOClient(socketURL: URL(string: "http://somesocketioserver.com")!, config: [.nsp("/client")])