title: How to only allow one user connection alive at the same time with Nodejs and Expressjs?
body: <p>I just need to manage the concurrence on my app built with <strong>nodejs</strong> on the top of the nestjs framework.</p>
  
  <p>As a far as I know, the most simplest way to do that is controlling that online <strong>one session user</strong> in <strong>expressjs</strong> is alive.</p>
  
  <p>I am not taking care about security or whatever other issues, just want to know <strong>how many users are connected</strong> and restricting it to <strong>only one user session</strong> till <strong>its session is expired</strong>.</p>
  
  <p>Here is my codebase</p>
  
  <pre><code>var express = require('express');
  var session = require('express-session');
  var app = express();
  
  var numConnections = 0;
  
  
  app.use(session({
      cookieName: 'sessionTest',
      secret: 'eg[isfd-8yF9-7w2315df{}+Ijsli;;to8', 
      cookie: {
        secure: false,
        maxAge: 1000 * 10,
        sameSite: true
      }
  }));
  
  
  app.use((req, res, next) =&gt; {
      console.log(req.session.store)
      console.log(req.session.ip)
      console.log(req.session.useragent)
      console.log(req.connection.remoteAddress)
      console.log(req.headers['user-agent'])
      if (numConnections === 0 
          // &amp;&amp; req.session
          ) {
          req.session.ip = req.connection.remoteAddress;
          req.session.useragent = req.headers['user-agent'];
          req.session.page_views = 1;
          res.send("Welcome to this page for the first time!");
          numConnections++;
          console.log(req.session);
          next();
      }
      else if (numConnections == 1 &amp;&amp;
          req.session.ip === req.connection.remoteAddress
          &amp;&amp; req.session.useragent === req.headers['user-agent']
      ) {
          req.session.page_views++;
          res.send("You visited this page " + req.session.page_views + " times");
          console.log('TEST');
          next();
      } else {
          console.log('There is someone using the app!!!');
          return res.sendStatus(401);
      }
  
  })
  
  app.listen(3001);
  </code></pre>
  
  <p>I really appreciate if someone can help me</p>