title: Is it okay to create a net.Socket() 'data' event handler once for each Node.js http request/response?
body: <p>I've got a Node.js web server communicating with a locally running Python TCP socket server (communicating via their respective socket modules <a href="https://nodejs.org/api/net.html#net_class_net_socket" rel="nofollow noreferrer" title="Node.js">net.Socket</a>, <a href="https://docs.python.org/2/library/socket.html" rel="nofollow noreferrer" title="Python2">socket</a>). </p>
  
  <p>Clients make HTTP post requests from the browser which get handled by a Node <code>http.createServer</code> function, with some of them sent to the Python server for heavy computations, the results of which are then sent back to Node and back to the browser for rendering.</p>
  
  <p>The Python server is necessary instead of a Node child process as there are some large (immutable) objects required for the Python computations that take a while to initialise and are then shared across threads. It would be infeasible to create and destroy these objects for every browser request.</p>
  
  <p>So my question is, using the Node callback paradigm, how do I capture the <code>response</code> object for each POST <code>request</code> in the <code>net.Socket</code> data event handler/s?</p>
  
  <p><strong>Note 0:</strong> Each request has a unique id that is sent to the Python server and returned.</p>
  
  <p>This currently works* inside my <code>http.createServer</code> callback:</p>
  
  <pre><code>http.createServer((request, response) =&gt; {
    // route and parse incoming requests etc.
  
    // send POST data to Python
    python_socket.write(parsed_request_post_data);
  
    // Python works away diligently then emits a data event handled below
    python_socket.once('data', (data_from_python) =&gt; {
      // error and exception handling
      response.setHeader('Content-Type', 'application/json');
      response.end(data_from_python);
    });
  }).listen(HTTPport);
  </code></pre>
  
  <p>*However if I bomb the server with multiple requests, sometimes I get the same data returned in each response (even though Python handles each data separately). I worry that I am trying to assign multiple <code>once('data'</code> callbacks in the same Node event loop and only one of them is persisting, and that is the one repeatedly sending the Python data back to the browser? Though if this were the case the <code>response</code> object would also be getting repeated and I would get an error for trying to end an already closed response right? But each response seems to end fine.</p>
  
  <p>Apologies for the rather long and vague question. I'm still learning and would really appreciate any advice or references I can study to help me understand what is going on. Also very open to trying different approaches (except changing web server - see note 2 below).</p>
  
  <p><strong>Note 1:</strong> I tried declaring a global data handler (note the <code>on</code> instead of <code>once</code>) for the <code>net.Socket</code> server as follows, but couldn't figure out how to forward the returned data to each http response?</p>
  
  <pre><code>python_socket.on('data', (data_from_python) =&gt; {
    // error and exception handling
    // how do I get data_from_python out to each http response
    // then close it in a non-blocking way?
  });
  </code></pre>
  
  <p><strong>Note 2:</strong> I'm not allowed to use a Python web server as the business wants to reuse this design in future to plug and play other services (R, Julia, C++, ...) into Node web servers.</p>