title: emit function from client side to all room only once, socket.io
body: <p>I am a newbie with socket.io and I have a hard time figuring out how to correctly use server and client. So, I have a function on my client side that listens to the server to get an array of questions to display it to the room.</p>
  <p>The idea is to create a room with several clients connected, and to emit to one room a function from the client side (only once, as I want to display the information to all the room). I really need this function on my client side as I have approximately 100 lines of code that concerns the front. Also my server needs to emit its array to the client.
  My problem is, since it is client side, when I emit it to the server it will be emitted  x times the number of clients inside the room. And I wish to emit it only once.</p>
  <p>To have a concrete example : My server has an object of array which is passed to the clients in a room so they can display it. Here is the function in the client :</p>
  <pre><code>//I have shorten the function so it is not too much but you can still grasp how I need it in my client
  socket.on('getNewQuestion', (currentQuestion) =&gt; {
  
  init();
  questionNumber.innerHTML = &quot;Question &quot; + countQuestion;
  questionParams.id = currentQuestion.questionId;
  questionParams.questionTxt = currentQuestion.question;
  questionParams.reponse = JSON.parse(currentQuestion.reponse);
  questionParams.lien = currentQuestion.lien;
  questionParams.type = currentQuestion.type;
  
  //Display question text
  questionTxt.innerHTML = currentQuestion.question;
  
  //Display different informations according to the question's type
  switch (currentQuestion.type) {
      case &quot;enigme&quot;:
          var getReponseLength = JSON.parse(currentQuestion.reponse)[0].length;
          questionType.innerHTML = &quot;find the answer to this question with &quot; + getReponseLength + &quot; letters&quot;;
  
          break;
  
      case &quot;anagramme&quot;:
  
          questionType.innerHTML = &quot;find a word usign all these letters :&quot;;
          const html = [];
          //Parse string to create array of chars
          var questionParse = JSON.parse(currentQuestion.question);
          //Gets the characters of the string and shuffles it 
          for (let i = questionParse.length - 1; i &gt;= 0; i--) {
              const randLetters = Math.floor(Math.random() * (i + 1));
              [questionParse[i], questionParse[randLetters]] = [questionParse[randLetters], questionParse[i]];
              html.push(`&lt;div&gt;${questionParse[i]}&lt;/div&gt;`);//display letter
          }
          currentQuestion.question = questionParse;
          questionTxt.innerHTML = html.join(&quot; &quot;);
          break;
  }
  countQuestion++;
  });
  </code></pre>
  <p>Then I emit it in my server to get the object of question</p>
  <pre><code>  socket.on('callGetNewQuestion', (roomCode) =&gt; {
      //Get a random question from the object
      const questionCalled = questions.getQuestionsList(roomCode)[Math.floor(Math.random() * questions.getQuestionsList(roomCode).length)];
      io.in(roomCode).emit('getNewQuestion', questionCalled); //emit the random question to the client
      questions.removeQuestion(questionCalled.questionId); //remove it so it doesn't appear twice
    });
  </code></pre>
  <p>And I am emitting back in my client (which I am not sure if it is good practice but I don't have any idea on how to listen to the object otherwise)</p>
  <pre><code>socket.on('startGame', () =&gt; {
      socket.emit('callGetNewQuestion', userParams.roomCode);
      timerCountdownGame();
  });
  </code></pre>
  <p>The problem with this code is : as it is called from the client side, if there is two clients in the room it is called twice, if there are three clients, it is called three times, so when I start the game it will pass from question 1 to question 3 (if number of clients is three).
  I hope what I am saying makes sense, if not do not hesitate to tell me</p>
  <p>Does anyone know how I can correct it ?</p>