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?
\0before the name if you want to connect or create to an abstract namespace unix domain socket. man UNIX(7):(abstract: an abstract socket address is distinguished (from a pathname socket) by the fact that sun_path[0] is a null byte ('\0')) So it might work if you use either\0#user#5632#1or\0@#user#5632#1. But I never tried that myself. - t.niese