title: Node.js + Socket.io Maximum call stack size exceeded
body: <p>For my Node application, I have a server (app.js) running on Debian serving both html and websocket data using socket.io to my client (index.html). I am attempting to make a turn-based HTML5 multiplayer game.</p>
  
  <p>After performing a number of successful data transmissions using socket.emit()/io.emit() and socket.on(), my server crashes on an socket.emit() call with the error 
  <br>"events.js:72
  <br>throw er; //Unhandled 'error' event
  <br>RangeError: Maximum call stack size exceeded".
   <br>I have quite a few socket.on() event listeners, with each one handling a different function in the game (e.g. roll_dice, end_turn, ready_to_play, etc.).</p>
  
  <p>I attempted researching the problem (found a lot of discussion on async loops), but was unable to find how to apply the solutions to my own code. I attached relevant source here. You can also view all the source on my github at: <a href="https://github.com/sjmoon0/gameofdeath" rel="noreferrer">https://github.com/sjmoon0/gameofdeath</a></p>
  
  <p>index.html
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var socket = io.connect('http://131.178.15.173',{'forceNew':true});
   
                      ...
  
    //----------------Initialization and Menu functions-----------
    socket.on('load', function (data) {
      console.log(data);
      clientID=data;
      socket.emit('check_game_started', { un: clientID });
      socket.on('last_client_loaded', function(hasStarted){
       console.log("Has game started? :"+hasStarted);
       if(hasStarted==true){
        $('#choosecharacter').show();
       }
      });
    });
  
    socket.on('client_disconnect', function (data) {
      console.log(data);
    });
  
    socket.on('client_counter', function (data) {
      if(data&lt;5){
       console.log(data);
       incrementLoadBar(data); 
       allowedInGame=true;
      }
      if(!allowedInGame){
       ...
      }
    });
  
    socket.on('game_started', function (data) {
      console.log(data);
      $('#welcome').hide();
      $('#choosecharacter').show();
    });
  
    socket.on('set_user', function(characterName){
     chosenCharacter=characterName;
    });
  
    socket.on('disable_player_choice', function(data){
     var id=data.stuff[0].chara;
     incrementLoadBar(data.stuff[0].numChar);
     console.log(id +" was chosen");
     $('#'+id).hide();
    });
  
  
  //-------------------Gameplay functions
    socket.on('start_gameplay',function(nonsense){
     showChanges(nonsense);
   $('#wait').hide();
   $('#gamespace').show();
   draw_c();
   socket.emit('ready_to_play',chosenCharacter);
    });
  
    socket.on('take_turn',function(updatedBoard){
     showChanges(updatedBoard);
     if(updatedBoard.currPlayer==chosenCharacter){
      promptUser(updatedBoard);
     }
    });
  
    socket.on('roll_result',function(rollResult){
     promptUser(rollResult);
    });
  
                    ...
                    
  
   $('#rollDiceButton').click(function(){
    socket.emit('roll_dice',chosenCharacter);
   });
  
   $('#okCloseButton').click(function(){
    socket.emit('end_turn',chosenCharacter);
   });
  
   $('.thumbnail').click(function(something){
    socket.emit('player_chosen', something.target.id);
                ...
   });</code></pre>
  </div>
  </div>
  </p>
  
  <p>app.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var app = require('http').createServer(handler)
  var io = require('socket.io')(app);
  var fs = require('fs');
  var url = require('url');
  
  ...
  
  app.listen(8001);
  
  function handler (req, res) {
  ...
  }
  console.log("~Server Running~");
  
  
  io.on('connection', function (socket) {
    console.log("A Client connected");
    ...
    socket.emit('load', { user: uID });
    io.emit('client_counter',numClients);
  
    if(numClients==4){
        gameStarted=true;
        console.log("Game started!");
        io.emit('game_started',"The Game has begun!");
      }
      else if(numClients&gt;4){
        numClients--;
        delete allClients[allClients.indexOf(socket)];
      }
  
    socket.on('check_game_started', function (data) {
      socket.emit('last_client_loaded', gameStarted);
      console.log(data);
      if(gameStarted){
        console.log("Last Player Loaded!");
      }
    });
  
    socket.on('player_chosen', function(cp){
      ...
      socket.emit('set_user', cp);
      ...
      io.emit('disable_player_choice',{'stuff':[{'chara':cp,'numChar':numCharChosen}]});
      if(numCharChosen==4){
        io.emit('start_gameplay', boardUpdate);
      }
    });
  
    socket.on('disconnect',function(){
      console.log("A client disconnected");
      numClients--;
      delete allClients[allClients.indexOf(socket)];
      io.emit('client_disconnect',"We've lost another comrade!");
    });
  
    socket.on('ready_to_play',function(characterThatIsReadyToPlay){
      io.emit('take_turn',boardUpdate);
    });
  
    socket.on('roll_dice', function(characterThatRolledDice){
      var temp=generateRollResult(characterThatRolledDice)
      socket.emit('roll_result',temp);
    });
  
    socket.on('end_turn',function(characterThatEndedTurn){
      io.emit('take_turn',nextUpdate(characterThatEndedTurn));
    });
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Please be gentle, I just started using Node.js about a week ago. Thanks!</p>