title: socket.io - middleware vs running after connection
body: <p>I'm working with socket.io for the first time to setup auth.</p>
  <p>Whats the difference between using a <strong>middleware</strong> vs <strong>running a function after connection</strong>?</p>
  <p>Ref: <a href="https://socket.io/docs/v4/middlewares/" rel="nofollow noreferrer">https://socket.io/docs/v4/middlewares/</a></p>
  <pre><code>// middleware
  io.use((s, next) =&gt; {
   if (isAuthed(s.request)) {
    next();
   } else {
    next(new Error(&quot;invalid&quot;));
   }
  });
  </code></pre>
  <p>vs</p>
  <pre><code>// after connection
  io.of(&quot;/&quot;).on(&quot;connection&quot;, (s) =&gt; {
   if (!isAuthed(s.request)) throw new Error(&quot;invalid&quot;);
   ...
  });
  </code></pre>