title: nodejs socket.io action handler with a for loop
body: <p>I sincerely hope that my following question is clear.</p>
  
  <p>Let's say I have 10 users connected to my server, each with a LCD touch screen display.
  They have a choice to click from a selection of buttons, in which those buttons emits some data to the server. Then the server processes this data and then emits it back to certain client(s).</p>
  
  <p>Each button (in my case are <code>&lt;a&gt;</code> tags styled to look like buttons) executes a function that looks something like the one below:</p>
  
  <pre><code>function emitExample(action, clickedTag){
      socket.emit(userID, {action:action, data: "data"});
  }
  </code></pre>
  
  <p>Then on the server, I want to be able to handle all of these requests without "socket.broadcast.emit()" or "socket.on()" for every single request, because in the future, I will have many more users and possible requests.</p>
  
  <p>Let's consider this: I need to recognize the combo user-buttonTag so I would need to have something like this: </p>
  
  <pre><code>    //where to add new users
      var users = {
              "user-1":"user-1",
              "user-2":"user-2",
              ...
              ...
              "user-N":"user-N"
          };
  
      //where to add new actions
      var actions = {
              'action-1':'action-1',
              ...
              ...
              'action-N':'action-N'
              };
  
      for(var action in actions)
      {
          for(var user in users)
          {
              // *** here i have the issue *** //
              events[key] = function(data)
              {
                  socket.broadcast.emit(action, data);
              };
          }
      }
  
      // this work fine...
      for (var method in events) {
          var dynamicHandler = function (realMethod) 
          {
              socket.on(realMethod, function (data) {
                  events[realMethod].apply(this, data);
              });
          };
          dynamicHandler(method);
      }
  </code></pre>
  
  <p>Around the end:</p>
  
  <pre><code>events[key] = function(data)
  </code></pre>
  
  <p>I would like to make all the user/action combination working, however it only retrieves the last one.</p>
  
  <p>I would greatly appreciate it if you could help me. :)</p>