title: Why does Socket.io emit empty events at random intervals?
body: <p><a href="https://i.stack.imgur.com/eR6o5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eR6o5.png" alt="enter image description here"></a></p>
  
  <p>I have an application that runs on Nodejs and Socket.io</p>
  
  <p>The events 2, 3 seen in the image are empty events that socket.io server keeps emitting at random intervals. What's more annoying is that the time delay between the empty events is crazy, as seen in the picture there was a 19 seconds time delay between 2 and 3. And due to this, the message:delete event that was emitted by socket.io client which was supposed to immediately emit message:deleted, got delayed by 19 seconds!!</p>
  
  <p>What's worse is that while these empty events are emitted, all other socket events originated from socket client are not emitted and they are stuck in limbo until the empty events are out of the way.</p>
  
  <p>I suspect these empty events are socket <code>ping</code> and <code>pong</code> events. Does anyone have any idea why this happens? And how we could prevent this? Or maybe even set a priority for client socket.emit so that regardless of these empty events, the client emitted socket event takes precedence and is fired immediately?</p>
  
  <p>Edit #1</p>
  
  <p>Despite adding <code>{ transports : ['websocket'] }</code> the delay was still well over 17 seconds as you can see in this image</p>
  
  <p><a href="https://i.stack.imgur.com/xj2g2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xj2g2.png" alt="enter image description here"></a></p>
  
  <p>Edit #2 - The socket.emit code</p>
  
  <p>The client is made up of a lot of jQuery + vanilla JS + Angular 1.7. The client socket is a factory wrapper</p>
  
  <pre><code>app.factory('socket',['$rootScope', function ($rootScope) {
  var socket = io.connect('',{
      path: '/socket.io',
      transports: ['websocket']
  });
  return {
      on: function (eventName, callback) {
          socket.on(eventName, function () {
              var args = arguments;
              $rootScope.$apply(function () {
                  callback.apply(socket, args);
              });
          });
      },
      emit: function (eventName, data, callback) {
          socket.emit(eventName, data, function () {
              var args = arguments;
              $rootScope.$apply(function () {
                  if (callback) {
                      callback.apply(socket, args);
                  }
              });
          })
      },
      disconnect: function(close){
          socket.disconnect(close);
      },
      removeAllListeners: function (eventName, callback) {
          socket.removeAllListeners(eventName, function() {
              var args = arguments;
              $rootScope.$apply(function () {
                  callback.apply(socket, args);
              });
          });
      }
  };
  }])
  </code></pre>
  
  <p>And on the controller the `socket.emit' code is as follows</p>
  
  <pre><code>$scope.deleteMessage = function (message) {
    socket.emit('message:delete', {
      id: message
    });
  };
  </code></pre>
  
  <p>I use express + Nodejs so here's the code for initializing the server socket</p>
  
  <pre><code>Server = require('socket.io'),
  io = new Server({
    path: `/socket.io`,
    transports: ['websocket']
  });
  </code></pre>
  
  <p>And the server side socket code listens to the client emit as follows</p>
  
  <pre><code>io.on("connection", async function (socket) {
    socket.on("message:delete", function (payload) {
      if (props.connectedUsers[socket.userId].info.isAdmin) {
        console.log("Received Message Deletion by", 
        props.connectedUsers[socket.userId].info.name);
  
        if (payload.id == "SYS_MOTD") return;
        await Message.remove({
          _id: new db.Types.ObjectId(payload.id)
        }).exec(function (err, message) {
          if (!err) {
            console.log("Emit message:deleted");
            io.sockets.emit("message:deleted", {
              id: payload.id
            });
          }
        });
      }
    });
  });
  </code></pre>