title: Apache Reverse Proxy with Node JS and Socket IO spawning lots of httpd_prefork processes
body: <p>I'm attempting to setup a reverse proxy using Apache for my Node JS app, which uses Socket IO (and is written in Koa v1.x).</p>
  
  <p>This works great if I <em>don't</em> use Socket IO; but when I do use it, I am running into big issues with Apache spawning lots of http_prefork processes - to such an extent, that it maxes them out (my config is 150), and Apache just hangs forever until I restart it.</p>
  
  <p>My hunch is that Socket IO is keeping the processes open, so when my app gets more than 150 connections, it gets upset and hangs - leading me to believe I am doing something wrong...</p>
  
  <p>Has anyone else experienced the same issue as me, and if so - is it resolvable, or do I simply up the maximum number of prefork processes indefinitely (feels wrong).</p>
  
  <p>I'd normally use Ngnix for reverse proxy - it seems to play nicer in my experience (and I haven't ran into this issue) - but unfortunately, installing it on the server my app is running on is out of the question.</p>
  
  <p>Here's a part of my apache config:</p>
  
  <pre><code>ProxyRequests        off
  ProxyPreserveHost    on
  RewriteEngine        on
  
  RewriteCond         %{QUERY_STRING}     transport=polling           [NC]
  RewriteRule         /(.*)               http://localhost:5000/$1    [P]
  RewriteCond         %{HTTP:Upgrade}     websocket                   [NC]
  RewriteRule         /(.*)               ws://localhost:5000/$1      [P]
  ProxyPass           /my-app/            http://localhost:5000/
  ProxyPassReverse    /my-app/            http://localhost:5000/
  
  # Redirect to fully-qualified domain name for HTTPS
  Redirect permanent  /my-app             https://[fqdn]/my-app/
  </code></pre>
  
  <p>Here's a couple of snippets of my Koa app too and how I use Socket IO.</p>
  
  <p><em>I've removed some parts to avoid over complicating, but can include more if required.</em></p>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>'use strict';
  const app    = koa();
  const server = http.createServer(app.callback()).listen(process.env.PORT || 5000, =&gt; {
      console.log('HTTP Server started...');
      const io = require('./io')(server);
  });
  </code></pre>
  
  <p><strong>io.js</strong></p>
  
  <pre><code>module.exports = (server) =&gt; {
      const io = require("socket.io")(server);
  
      // Catch errors
      io.on("error", (err) =&gt; {
          console.error(err);
      });
  
      // On connection
      io.on("connection", (socket) =&gt; {
  
          console.log("[SOCKET IO]: A user has connected with ID:", socket.id);
  
          // Join a room event with callback emit of 'room-joined'
          socket.on('join-room', (roomName) =&gt; {
              console.log(`A user is joining a room with the name ${roomName}`);
              socket.join(roomName);
              socket.emit('room-joined', roomName);
          });
  
      });
  </code></pre>