Search code examples
javascriptnode.jsunix-socket

Connecting to Named Unix Domain Socket Node


I am trying to connect to a named Unix Domain Socket via nodejs. I have seen that the docs seem to support connecting to Unix Sockets, however I haven't seen any examples of connecting to a socket by name, and not by accessing a socket file at a well known location.

I can clearly see that the socket I need to connect to is being created by using lsof (and some grepping):

COMMAND    PID  USER   FD   TYPE             DEVICE SIZE/OFF  NODE NAME
run       5632 user    3u  unix 0xffff8803dd4b6000      0t0 29647 @#user#5632#1 type=SEQPACKET

The name of the socket is being passed to the script that actually runs my node script. I have tried the following:

import net = require('net');
var socket;
var element = "#user#5632#1"; //Parsed from args
try {
    socket = net.createConnection("@"+element,(error)=>{
        if(error){
            console.log(error)
        }else{
            console.log("Connection Established "+element);}
        });
        socket.on('error', function(err) {
            console.log("Error: " + err);
        });
    } catch (error) {
        console.error(error);
    }

Clearly, I have this wrapped up to catch errors in a few places(at different points in execution), but the point is that createConnection is throwing:

Error: Error: connect ENOENT @#user#5882#1

Using net.connectcauses the exact same error.

I tested creating a socket (file) in a well known location, and connecting to is, and that worked just fine, but as far as I have determined, node, or at least the net module does not seem to support connecting to ephemeral sockets.

Anybody know if there is a way to do this, or if I need to format my socket name differently in order to connect or any help really?


Solution

  • Node does not support SEQPACKET sockets. You may submit a PR to the node issue tracker as a feature request to add support and/or you may need to write a node addon that lets you connect to SEQPACKET sockets.