I hate it when documentation doesn’t advocate best practices. Even worse, that means that virtually every tutorial on the bleeding internet copypastas those same bad practices. I’m looking at you here, socket.io!
What’s the matter? All official examples – and, hence, the entire interwebz – use the following structure with anonymous functions:
io.on('connection', function (socket) {
socket.on('some-event', function () {
//...do something...
});
});
Seems reasonable, right? I used this same approach in our dating-app, since that’s what everyone recommended. Who wouldn’t?
Well, woe on me.
A few days ago, as the app started gaining some traction, I started getting surprised by some weird out of memory errors on the webserver. “That’s odd”, I thought to myself, “there’s some traction but no way should it cause these errors just yet. Isn’t socket.io supposed to be, like, super-efficient?”
Well, yeah, but not when you’re Doing It Wrong.
See, using anonymous functions comes at a price: NodeJS creates a copy of them each time. In other words, each client was consuming memory for every event (and FlirtTracker has about 50 of them). So yeah, once I realised that the memory usage started making sense.
The solution? Use named functions declared just once (or, as I did ES6-style, anonymous functions assigned to a constant). Now NodeJS uses references to them and the whole memory problem goes away (well, for now, I guess it’ll pop up again when we really get some traction but it would be premature optimization to worry about that now).
Something that’s also not very obvious from the official docs (at least, I didn’t spot it) is that the socket events are actually bound to the current socket. In my original code I was passing the socket object around to utility methods that then returned the actual handler. Something like this:
module.exports = socket => (data, callback) => {
// ...do something...
};
…and then in the main function run on connection:
socket.on('someEvent', require('./my-event')(socket));
But, because socket is bound, you can simply refer to it as this
inside the handler:
socket.on('someEvent', function () {
this.emit('someOtherEvent');
});
Actually, this solved a whole lot of other issues where I was passing all sorts of stuff around – I simply planted them on the socket in the main file, and they were now available via this
! Add in some smart helper methods on the socket and I was able to reduce memory usage even more. For example, we also use a remote object pointing to a DNode server. I optimized that to this:
const getRemote = () => remote;
io.on('connection', function (socket) {
socket.remote = getRemote;
});
(I used a method since I no longer trusted NodeJS to also use a reference of the object instead of a copy – I’m pretty sure Javascript passed objects as references like a good boy, but better safe than sorry.)
It even simplified our unit tests, for we could now use function.call
or function.apply
to bind a fake socket!
Last point to note: make sure the functions handling the events are actual functions, not arrow functions. I love arrow functions but one of their explicit features is they don’t have scope, so they can’t be bound to the socket. this
will simply be an empty object in that case.