Meteor.onConnection - onClose when deploying to Galaxy


#1

Hi everyone! =)

I have this piece of code on my application in order to always keep track of connected users and to take actions when they disconnect:

Meteor.onConnection(function(connection) {

    Presences.insert({
        _id: connection.id,
        address: connection.clientAddress,
    });

    connection.onClose(function() {
        Presences.remove(connection.id);
    });
});

and I get the userId when the user logs in:

Accounts.onLogin(function(info) {

    var connectionId = info.connection.id;
    var user = info.user;
    var userId = user._id;

    Presences.update(connectionId, {
        $set: {
            userId: userId,
            loginAt: new Date()
        }
    });
});

This work perfectly. But… when deploying a new version of my app, all users are disconnected on server restart and the onClose hook is not called in this situation.
The solution until now: when the server restarted after my deploy, I would just clean up the Presences collection and it would be repopulated once users slowly started to reconnect again:

Meteor.startup(function() {

    Presences.remove({});
});
```

This worked well when I was using just one EC2 server on AWS.

Now I'm using Galaxy and this solution doesn't work very well. I had to remove the ```Presences.remove({})``` piece of code because when Galaxy spins up a new container I cannot clean the entire ```Presences``` collection since there are still a lot of users connected to other containers. But now when I deploy a new version I end up with more than one object in the Presences collection for the same user. I have no way to clean old connection objects since the ```onClose``` hook was not called.

Does any one have any idea how to solve this problem? I just need to keep this ```Presences``` collection always up to date with the users connected to my application and the solution must be resilient to deploys or server restarts.

Thanks,
Alex