title: How to send new user the old sent messages with socket.io
body: <p>I am using <a href="https://dzone.com/articles/getting-started-with-nodejs-and-socketio" rel="noreferrer">this</a> tutorial. I need to send old messages to the new connected user. How can I do that? I mean the new users should be able to see the old messages.</p>
  
  <p><em>for my application, i don't need to store the data in a database. Once the server is turned off then the data should be removed. but how long the server runs on each session the clients should get previous message</em> </p>
  
  <p>I have tried </p>
  
  <pre><code> const history = []
  //Telling Express+Socket.io App To Listen To Port
  io.sockets.on("connection",function(socket){
  
      socket.emit("Start_Chat");
      //On Event Registar_Name
      socket.on("Register_Name",function(data){
              console.log(data)
         io.sockets.emit("r_name","&lt;strong&gt;"+data+"&lt;/strong&gt; Has Joined The Chat");
         //Now Listening To A Chat Message
         socket.on("Send_msg",function(data){
                 history.push(data)
                 console.log(history)
         io.sockets.emit("msg",data);
         //Now Listening To A Chat Message
      })
      })
  })
  </code></pre>
  
  <p>but how can I say when a new client enters send history data? </p>
  
  <p>UPDATE: I have tried the following but it is always sending the history</p>
  
  <pre><code> const history = []
   const client = []
  //Telling Express+Socket.io App To Listen To Port
  io.sockets.on("connection",function(socket){
  
          client.push({id : socket.client.id})
          console.log(client)
  
      socket.emit("Start_Chat");
      //On Event Registar_Name
      socket.on("Register_Name",function(data){
              console.log(data)
         io.sockets.emit("r_name","&lt;strong&gt;"+data+"&lt;/strong&gt; Has Joined The Chat");
         //Now Listening To A Chat Message
         socket.on("Send_msg",function(data){
  
                 history.push(data)
                 console.log(history)
                 if(client.find(e =&gt; e.id !== socket.client.id)) {
                  io.sockets.emit("msg",history);
                 } else {
                  io.sockets.emit("msg",data);
                 }
  
         //Now Listening To A Chat Message
      })
      })
  </code></pre>