title: Socket.IO Transport Close Trouble
body: <p>I'm having a lot of trouble with socket.io. I am coding this to be able to create a room, join it, and then use socket.io to play a game between the people. This is really just a coding challenge to see if I can code something using socket.io from scratch. The problem is that when one client disconnects, all of the other clients disconnect. Here is the code:</p>
  <pre><code>&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.1/socket.io.js&quot; integrity=&quot;sha512-MgkNs0gNdrnOM7k+0L+wgiRc5aLgl74sJQKbIWegVIMvVGPc1+gc1L2oK9Wf/D9pq58eqIJAxOonYPVE5UwUFA==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
  &lt;script&gt;
      
    var code;
    var codes;
    var socket = io();
    var log;
    var variable = false;
    var nick = null;
    var me = {};
    
    socket.on(&quot;com&quot;, function(type, arg1) {
      switch (type) {
        case &quot;joined&quot;: 
          document.getElementById(&quot;getKey&quot;).innerHTML = &quot;&quot;
        break;
        case &quot;dataSendCodes&quot;:
          codes = arg1;
        break;
        case &quot;dataSendLogs&quot;:
          log = arg1;
        break;
      }
    });
    
    socket.on('connect', function() {
      console.log(&quot;socket connected&quot;);
      socket.on(&quot;disconnect&quot;, function(reason) {
        console.log(&quot;disconnected - &quot; + reason);
        console.log(socket.id);
      });
    });
    socket.on('connect_error', function(error) {
      console.log(error);
    });
      
    function fetchcodes() {
      socket.emit(&quot;comm&quot;,&quot;getData&quot;);
      console.log(&quot;Data fetch request sent&quot;);
        if (log != undefined &amp; codes != undefined) {
          return true;
        } else {
          return false;
        }
    }
    
    function createRoom() {
        fetchcodes();
        setTimeout(function() {           
          createcode();
        }, 2500);
    }
    
    
    function createcode() {
      for (code = Math.floor(Math.random() * 99999999) + 1; codes[code] != undefined; code = Math.floor(Math.random() * 99999999) + 1) {
        console.log(&quot;NOPE!&quot;);
      }
        socket.emit(&quot;comm&quot;, 'createcode', code);
        document.getElementById(&quot;codeBox&quot;).innerHTML = &quot;&lt;p class='JoinText'&gt;Code: &quot; + code + &quot;&lt;/p&gt;&quot;
        socket.emit(&quot;comm&quot;, &quot;join&quot;, code, nick, true);
        me.nick = nick;
        me.id = socket.id;
        me.room = code;
        me.admin = true;
        console.log(me.room);
      }
    
  &lt;/script&gt;
  </code></pre>
  <p>And server side:</p>
  <pre><code>// server.js
  // where your node app starts
  
  // we've started you off with Express (https://expressjs.com/)
  // but feel free to use whatever libraries or frameworks you'd like 
  through `package.json`.
  const express = require(&quot;express&quot;);
  const app = express();
  const server = require(&quot;http&quot;).Server(app);
  const io = require(&quot;socket.io&quot;)(server);
  
  // make all the files in 'public' available
  // https://expressjs.com/en/starter/static-files.html
  app.use(express.static(&quot;public&quot;));
  
  // https://expressjs.com/en/starter/basic-routing.html
  app.get(&quot;/&quot;, (request, response) =&gt; {
    response.sendFile(__dirname + &quot;/join.html&quot;);
  });
  
  app.get(&quot;/admin&quot;, (request, response) =&gt; {
    response.sendFile(__dirname + &quot;/twotruthsandalieadmin.html&quot;);
  });
  
  var Codes = {};
  
  var Log = {};
  
  io.on(&quot;connection&quot;, function (socket) {
    socket.on(&quot;disconnect&quot;, function() {
      var check = io.sockets.clients(Log[socket.id].room);
      if (check == 0) {
        delete Codes[Log[socket.id].room];
      }
      delete Log[socket.id];
    });
    socket.on(&quot;comm&quot;, function(type, arg1, arg2, arg3) {
      console.log(&quot;comm&quot;);
      switch (type) {
        case &quot;getData&quot;:
          console.log(&quot;get data&quot;);
          io.to(socket.id).emit(&quot;com&quot;,&quot;dataSendCodes&quot;,Codes);
          io.to(socket.id).emit(&quot;com&quot;,&quot;dataSendLogs&quot;,Log);
          console.log(&quot;emitted&quot;);
          break;
        case &quot;join&quot;:
          socket.join(arg1, function () {
            console.log(&quot;joined &quot; + socket.id + &quot;to a room&quot;);
            io.to(arg1).emit(&quot;com&quot;,&quot;joined&quot;);
          });
          Log[socket.id] = {nick: arg2,id: socket.id,room: arg1,admin: arg3};
        break;
        case &quot;createcode&quot;:
          Codes[arg1] = arg1;
        break;
      }
    });
  });
  
  function reset() {
    Log = {};
    Codes = {};
  }
  
  
  server.listen(process.env.PORT);
  </code></pre>
  <p>If it helps here are the dependencies:</p>
  <pre><code>{
    &quot;//1&quot;: &quot;describes your app and its dependencies&quot;,
    &quot;//2&quot;: &quot;https://docs.npmjs.com/files/package.json&quot;,
    &quot;//3&quot;: &quot;updating this file will download and update your packages&quot;,
    &quot;name&quot;: &quot;hello-express&quot;,
    &quot;version&quot;: &quot;0.0.1&quot;,
    &quot;description&quot;: &quot;A simple Node app built on Express, instantly up and running.&quot;,
   &quot;main&quot;: &quot;server.js&quot;,
    &quot;scripts&quot;: {
      &quot;start&quot;: &quot;node server.js&quot;
    },
    &quot;dependencies&quot;: {
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;socket.io&quot;: &quot;^4.4.1&quot;
    },
    &quot;engines&quot;: {
      &quot;node&quot;: &quot;12.x&quot;
    },
    &quot;repository&quot;: {
      &quot;url&quot;: &quot;https://glitch.com/edit/#!/hello-express&quot;
    },
    &quot;license&quot;: &quot;MIT&quot;,
    &quot;keywords&quot;: [
      &quot;node&quot;,
      &quot;glitch&quot;,
      &quot;express&quot;
    ]
  }
  </code></pre>
  <p>This is done on glitch, I don't know if that makes a difference. The disconnect reason is transport close, meaning it lost connection but it only happens when another client is disconnected. There isn't any code that should be closing the connection server side as far as I can see.</p>
  <p>Any help is appreciated!</p>