Socket.io message is sent, but event handler doesn't run

I have debugging turned on and I see

    engine.io-client:socket socket receive: type "message", data "2["subscription","53333668df2bade936513a38"]"

My client side code is:

    $(function() {
      socket.on('subscription', function(id){
        console.log("AHHH");
      });
    })

No console log appears. It looks like I'm receiving the message on the clientside. Why doesn't anything happen? My backend is node, in case that helps.

Turned out it was a namespacing issue. D'oh

In general that kind of problem can happen because of handler not being registered yet when the message is received. In your example that could happen since the handler is registered in $(document).ready but io.connect() is called somewhere else.

For example, having

io.on('connection', function(socket) {
  socket.emit('subscription', 123);
});

on server side and

$(function() {
  localStorage.debug = '*';
  var socket = io.connect();
  setTimeout(function () {
    socket.on('subscription', function(id){
      console.log("AHHH");
    });
  }, 1000);
})

would show the message in the debug log but the handler would not be called.