title: Connecting to an already established UNIX socket with node.js?
body: <p>I am working on a node.js application that will connect to a UNIX socket (on a Linux machine) and facilitate communication between a web page and that socket. So far, I have been able to create socket and communicate back and forth with this code in my main app.js:</p>
  
  <pre><code>var net = require('net');
  var fs = require('fs');
  var socketPath = '/tmp/mysocket';
  
  fs.stat(socketPath, function(err) {
      if (!err) fs.unlinkSync(socketPath);
      var unixServer = net.createServer(function(localSerialConnection) {
          localSerialConnection.on('data', function(data) {
              // data is a buffer from the socket
          });
          // write to socket with localSerialConnection.write()
      });
  
  unixServer.listen(socketPath);
  });
  </code></pre>
  
  <p>This code causes node.js to create a UNIX socket at <code>/tmp/mysocket</code> and I am getting good communication by testing with <code>nc -U /tmp/mysocket</code> on the command line. However...</p>
  
  <p>I want to establish a connection to an already existing UNIX socket from my node.js application. With my current code, if I create a socket from the command line (<code>nc -Ul /tmp/mysocket</code>), then run my node.js application, there is no communication between the socket and my application (The 'connect' event is not fired from node.js server object).</p>
  
  <p>Any tips on how to go about accomplishing this? My experiments with node.js function <code>net.createSocket</code> instead of <code>net.createServer</code> have so far failed and I'm not sure if that's even the right track.</p>