Socket.io does not close the socket on demand (and does not allow my swift app to reconnect again to the node.js server)
<p>I am playing around with <code>socket.io</code> library, I already implemented the server side (in <code>node.js</code>). I try to follow a very simple scenario:</p>

<p>1) user establishes connection </p>

<p>2) user connects to the room</p>

<p>3) user disconnects</p>

<p>4) user reconnects</p>

<p>I'm using ios app (written in <code>swift</code>) on a client side.</p>

<p>My server code is as follows:</p>

<pre><code>io.on('connection', function(clientSocket){
    console.log('a user connected here');

    clientSocket.on('disconnect', function(){
        console.log('user disconnected');
    });

    clientSocket.on('connectUser', function(username){
        clientSocket.join(username);
        console.log('user ' + username + ' connected to the room.');
    });
</code></pre>

<p>Now in my client side I created a class responsible for handling all socket stuff:</p>

<pre><code>class SocketIOManager: NSObject {
static let sharedInstance = SocketIOManager()
var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: serverURL)!, options: [.ForceNew(true)])

override init() {
    super.init()
}

func establishConnection() {
    socket.connect()
}


func closeConnection() {
    socket.disconnect()
}

func connectToServerWithNickname(nickname: String) {
    print("CONNECTING TO SOCKET SERVER \(nickname)")
    socket.emit("connectUser", nickname)
}
}
</code></pre>

<p>Now - further on - in my swift app (in <code>applicationDidBecomeActive</code>) I'm calling <code>establishConnection()</code> and then <code>SocketIOManager.sharedInstance.connectToServerWithNickname(username)</code>.</p>

<p>In my server console I see:</p>

<pre><code>a user connected here
user 571fc6818451630f5becda9c connected to the room.
</code></pre>

<p>In my <code>appDelegate</code> I implemented this function:</p>

<pre><code>func applicationWillResignActive(application: UIApplication) {
    SocketIOManager.sharedInstance.closeConnection()
    print("closing sockets")
}
</code></pre>

<p>so now when I put my app in the background I see <code>closing sockets</code> and in my server console I see <code>user disconnected</code>.</p>

<p>And now to the main problem - I want to reconnect user when he comes back to the app. Therefore I implemented this method:</p>

<pre><code>func applicationDidBecomeActive(application: UIApplication) {

    print("application is back to life")

    SocketIOManager.sharedInstance.establishConnection()
    SocketIOManager.sharedInstance.connectToServerWithNickname(username)

}
</code></pre>

<p>But now when I wake up the app from the background, in my server console I only see <code>a user connected here</code>. So this message:</p>

<pre><code>user 571fc6818451630f5becda9c connected to the room.
</code></pre>

<p>is missing and I assume it's because my app is not reconnecting to the socket. </p>

<p>Now from what I've read, <code>socket.io</code> is really buggy and people recommend switching to other libraries, but I would like to use it anyway. I found this issue on github <a href="https://github.com/socketio/socket.io-client/issues/251" rel="nofollow">https://github.com/socketio/socket.io-client/issues/251</a> and they recommend to use a specific parameter on client side. Following this plugin's page for swift <a href="https://github.com/nuclearace/Socket.IO-Client-Swift" rel="nofollow">https://github.com/nuclearace/Socket.IO-Client-Swift</a> I found the specific flag: </p>

<pre><code>case ForceNew(Bool) // Will a create a new engine for each connect. Useful if you find a bug in the engine related to reconnects
</code></pre>

<p>and that is why I'm constructing my socket on client side as:</p>

<pre><code>var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: serverURL)!, options: [.ForceNew(true)])
</code></pre>

<p>but yeah, even in the official code of the <code>Socket.IO-Client-Swift</code> library there's a comment above the disconnect method:</p>

<pre><code>/// Disconnects the socket. Only reconnect the same socket if you know what you're doing.
/// Will turn off automatic reconnects.
public func disconnect() {
    DefaultSocketLogger.Logger.log("Closing socket", type: logType)

    reconnects = false
    didDisconnect("Disconnect")
}
</code></pre>

<p>Does anyone know how could I handle that? I want to close the socket each time my app goes to the background and connect to it again when my app goes to the foreground. Thanks!</p>