title: Persiste NodeJS Sockets
body: <p><strong>Hello community,</strong>
  Since the morning I am faced with an idea not to say a problem I want to store clients (sockets) in order to re-invoke them later, I know that it is not too clear I will explain in detail:</p>
  <p>First I have a server interface (ServerSocket) <code>net.Server()</code> which will receive clients (sockets) <code>net.Socket()</code> and this one will be stored in a Map() with a unique ID for each client, and each time I want to communicate with one of them I call map.get(id).write ... or other function,</p>
  <p>Everything works fine until I close the server, automatically the sockets will be killed ... saying that I have found a solution to store the clients for my case (vuex) or localStorage to simplify what I want is when I restart the server and I invoke one of the client it will always be active.
  So my main questions:</p>
  <ul>
  <li>How can I keep clients still active after the server is closed? <br></li>
  <li>How can I Store sockets and check if they are active after restarting server?</li>
  </ul>
  <pre><code> var net = require(&quot;net&quot;);
   var server = new net.Server();
   var sockets = new Map();
      
      /**
       * This events is called when server is successfully listened.
       */
      server.on(&quot;listening&quot;, () =&gt; {
        console.log(&quot;Server Listen in port 4444&quot;);
      });
      
      /**
       * This events is called when error occur in server
       */
      server.on(&quot;error&quot;, (e) =&gt; {
        if (e.code === &quot;EADDRINUSE&quot;) {
          console.log(&quot;Address in use, retrying...&quot;);
        }
      });
      
      /**
       * This events is called when a new client is connected to server.
       */
      server.on(&quot;connection&quot;, (socket) =&gt; {
        var alreadyExist = false;
        sockets.forEach((soc) =&gt; {
          if (soc.remoteAddress === socket.remoteAddress) {
            alreadyExist = true;
          }
        });
        if (alreadyExist) {
          socket.end();
        } else {
          socket.setKeepAlive(true, Infinity);
          socket.setDefaultEncoding(&quot;utf8&quot;);
          socket.id = nanoid(10);
          sockets.set(socket.id, socket);
      
          socket.on(&quot;error&quot;, (e) =&gt; {
            console.log(e);
            if (e.code === &quot;ECONNRESET&quot;) {
              console.log(&quot;Socket end shell with CTRL+C&quot;);
              console.log(&quot;DEL[ERROR]: &quot; + socket.id);
            }
          });
      
          socket.on(&quot;close&quot;, () =&gt; {
            console.log(&quot;DEL[CLOSE]: &quot; + socket.id);
          });
      
          socket.on(&quot;end&quot;, () =&gt; {
            console.log(&quot;DEL[END]: &quot; + socket.id);
          });
      
          socket.on(&quot;timeout&quot;, () =&gt; {
            console.log(&quot;timeout !&quot;);
          });
      
          var child = sockets.get(res.id);
          child.write(/* HERE I SEND COMMAND NOT IMPORTANT ! */);
      
          socket.on(&quot;data&quot;, (data) =&gt; {
            console.log(&quot;Received data from socket &quot; + data);
          });
        }
      });
  
  </code></pre>