Can node.js
listen on UNIX socket? I did not find any documentation regarding this. I only saw the possibility of listening on a dedicated port.
Asked
Active
Viewed 5.5k times
62

Dan Grossman
- 51,866
- 10
- 112
- 101

Luc
- 16,604
- 34
- 121
- 183
2 Answers
84
To listen for incoming connections in node.js you want to use the net.server class.
The standard way of creating an instance of this class is with the net.createServer(...)
function. Once you have an instance of this class you use the server.listen(...)
function to tell the server where to actually listen.
If the first argument to listen is a number then nodejs will listen on a TCP/IP socket with that port number. However, if the first argument to listen is a string, then the server object will listen on a Unix socket at that path.
var net = require('net');
// This server listens on a Unix socket at /var/run/mysocket
var unixServer = net.createServer(function(client) {
// Do something with the client connection
});
unixServer.listen('/var/run/mysocket');
// This server listens on TCP/IP port 1234
var tcpServer = net.createServer(function(client) {
// Do something with the client connection
});
tcpServer.listen(1234);

joshperry
- 41,167
- 16
- 88
- 103
-
Would it be possible to use child process and socket.io to have a client connection that communicates with a server side application using this method? would a new port/path be needed in order to maintain a 1 to 1 client to app relationship? – tremor Nov 02 '13 at 02:24
-
18For those wondering, you can do this with [express 4.x](https://expressjs.com) too! Because `app.listen` is [basically a helper function](https://expressjs.com/en/4x/api.html#app.listen), doing `app.listen("/var/run/mysocket")` is just as valid. – Sam Holmes Aug 13 '16 at 21:14
-
1it seems that the socket file remains after the execution exits. Is there a better way for node to auto remove the sock file when it exits? – ggedde Oct 16 '19 at 09:14
-
@ggedde you could add a process exit listener and then delete the socket file in that – derpedy-doo Apr 10 '20 at 00:18
-
2@ggedde I also have this problem. Putting `fs.unlinkSync(socketPath);` before the `listen()` call worked for me. – gitaarik Apr 07 '21 at 19:57
-
@electrovir and gitaarik, can you provide examples. So far adding the fs.unlinkSync(socketPath); before the listen only removes the socket before a start, but does not remove it on process end. I have also been looking into process exit listener, but can't find any good examples. Thanks – ggedde Sep 09 '22 at 20:49
-
1@ggedde I created a package back in the day to help with adding process exit listeners: https://www.npmjs.com/package/catch-exit The relevant source code for attaching listeners is here: https://github.com/electrovir/catch-exit/blob/master/src/index.ts#L223-L249 – derpedy-doo Sep 11 '22 at 13:04
33
Yes. It's in the documentation.
https://nodejs.org/api/net.html#net_server_listen_path_backlog_callback

Jesús Carrera
- 11,275
- 4
- 63
- 55

Dan Grossman
- 51,866
- 10
- 112
- 101
-
1For the http module https://nodejs.org/api/http.html#http_server_listen_path_callback – Ryan Allen Jun 04 '15 at 18:47