title: Getting Double response in socket.io on emit
body: <p>I am making a chat application in node js and socket.io . What I have don is, when a user click a link to go to the inbox of a user he or she chatted with, on that click even I send the id of that current user who clicked target chat to open its messages and the id of the target user who he or she chatted with through these two ids I am retrieving the messages of every user. But the problem is, on the first click the messages retrieved is the array of objects which works fine, but when the same target has been clicked again the messages array retrieved is twice in number and each time it is clicked it double the number of arrays and retrieves them. </p>
  
  <p>TO Better understand , have a look at the snap shot. You can see that on each click the arrays retrieved are double in number. </p>
  
  <p><a href="https://i.stack.imgur.com/htdCT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/htdCT.png" alt="enter image description here"></a></p>
  
  <p><strong>My Code - NodeJs One</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>socket.on("get_user_messages",(data)=&gt;{
            console.log(data);
        var GET_CLICKED_USER_MESS = "SELECT mess_txt,mess_time,mess_to FROM (select mess_to as user_id,mess_txt,mess_time,mess_id,mess_to from messages where mess_from = '"+data.active+"' AND mess_to='"+data.clicked+"' UNION select mess_to as user_id,mess_txt,mess_time,mess_id,mess_to from messages where mess_from = '"+data.clicked+"' AND mess_to='"+data.active+"' ORDER BY mess_id ASC) sq join users on users.id = sq.user_id";
            con.query(GET_CLICKED_USER_MESS,(err,res)=&gt;{
               if(err) throw err;
               socket.emit("get_ret_messages",result);
  
            })
         });</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>JavaScript where it is received</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var d = document, _id, mess, messs, chatBody = document.getElementsByClassName("indChatBody")[0], result;
       
         d.addEventListener("click",(e)=&gt;{
            e.preventDefault();
            var event = e.target;
            console.log("clicked");
                if(event.parentNode.className=="indUser" || event.parentNode.className=="ind_user_messages"){
                 if(event.parentNode.className=="ind_user_messages"){
                  _id = event.parentNode.getAttribute("data-id");
                 } else if(event.parentNode.className=="indUser"){
                  _id = event.getAttribute("data-id");
                 }
  
                   socket.emit("get_user_messages",{clicked:_id,active:data.active_user});
  
                 socket.on("get_ret_messages",(data)=&gt;{
                   result = [];
  
                   /*for(let i = 0; i&lt;data.length;i++){
                    if(data[i].mess_to == data.active_user){
                     mess = '&lt;span class="mess fromMess"&gt;&lt;div class="textTime"&gt;&lt;span class="messTime"&gt;'+data[i].mess_time+'&lt;/span&gt;&lt;/div&gt;&lt;p class="senderText"&gt;'+data[i].mess_txt+'&lt;/p&gt;&lt;/span&gt;';
                          chatBody.insertAdjacentHTML("beforeend",mess);
                       
                    }
                    messs = '&lt;span class="mess toMess"&gt;&lt;div class="textTime"&gt;&lt;span class="messTime"&gt;'+data[i].mess_time+'&lt;/span&gt;&lt;/div&gt;&lt;p class="senderText"&gt;'+data[i].mess_txt+'&lt;/p&gt;&lt;/span&gt;';
                       chatBody.insertAdjacentHTML("beforeend",messs);
                  }*/
                  result = data;
                  console.log(result);
                 });
  
                }
  
  
         })</code></pre>
  </div>
  </div>
  </p>