title: Socket.io: How to limit the size of emitted data from client to the websocket server
body: <p>I have a node.js server with socket.io. My clients use socket.io to connect to the node.js server.</p>
  
  <p>Data is transmitted from clients to server in the following way:</p>
  
  <p>On the client</p>
  
  <pre><code>var Data = {'data1':'somedata1', 'data2':'somedata2'};
  socket.emit('SendToServer', Data);
  </code></pre>
  
  <p>On the server</p>
  
  <pre><code>socket.on('SendToServer', function(Data) {
      for (var key in Data) {
             // Do some work with Data[key]
      }
  });
  </code></pre>
  
  <p>Suppose that somebody modifies his client and emits to the server a really big chunk of data. For example:</p>
  
  <pre><code>var Data = {'data1':'somedata1', 'data2':'somedata2', ...and so on until he reach for example 'data100000':'data100000'};
  socket.emit('SendToServer', Data);
  </code></pre>
  
  <p>Because of this loop on the server...</p>
  
  <pre><code>for (var key in Data) {
         // Do some work with Data[key]
  }
  </code></pre>
  
  <p>... the server would take a very long time to loop through all this data.</p>
  
  <p>So, what is the best solution to prevent such scenarios?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I used this function to validate the object:</p>
  
  <pre><code>function ValidateObject(obj) {
      var i = 0;
      for(var key in obj) {
          i++;
          if (i &gt; 10) { // object is too big
              return false;
          }
      }
      return false;
  }
  </code></pre>