6

Quick and basic nodeJs question, I'm working with unix socket for inter-server communication between c++ application and my NodeJs server,

I've wrote my nodeJs server like so:

var net = require('net');
var unixSocketServer = net.createConnection('/tmp/unixSocket');
unixSocketServer.on('connect',function(){
    console.log('unix server socket connected on /tmp/unixSocket');
    ...
});

However I'm getting connection refuse error. I can understand that the c++ application haven't opened/connected to the socket yet. My questions are why does it matter? shouldn't the nodeJs server wait until 'connect' event emitted? Am I using nodeJs currently? am I missing something?

3
  • 1
    Are you trying to start a server on a unix socket or connect to an existing unix socket? If the latter, did you check permissions on /tmp/unixSocket? If the former, you should use net.createServer() instead. Commented Jan 5, 2015 at 16:37
  • I'm having some trouble figuring this out, My usecase is that the c++ application internal state can be changed by the user via nodeJs. should this NodeJs be a server or a client? Commented Jan 5, 2015 at 16:55
  • My unix socket was previously declared, I've removed it with: fs.unlink(socketpath). Commented Jan 7, 2015 at 12:46

1 Answer 1

13

Ok, so there's some misunderstanding here. Let's start from what a 'unix socket' really is. I think you're thinking it's just a file-like item that acts like a server on its own through the OS/filesystem. That's not quite correct. While it is indeed bound through the filesystem, it isn't really a traditional file. Instead, it's just like an TCP/IP socket, except instead of binding an IP and port, a filepath is bound.

The key point there is that it's just a bound socket with a different type of address (and some extra capabilities, but that's outside the scope here). So that means that something has to bind the socket! In this case, we need a server, just like we would if we were communicating over a 'normal' port. If there isn't a server bound to the path, you get an error, just like you would when connecting to a port with no listener.

To create a server on a unix domain socket in node, it's pretty simple:

'use strict';

const net = require('net');
const unixSocketServer = net.createServer();

unixSocketServer.listen('/tmp/unixSocket', () => {
  console.log('now listening');
});

unixSocketServer.on('connection', (s) => {
  console.log('got connection!');
  s.write('hello world');
  s.end();
});

Note that there is one other difference between 'normal' sockets and unix domain sockets: After a server is done with the unix domain socket, it is not automatically destroyed. You must instead unlink /tmp/unixSocket in order to reuse that 'address'/path

Sign up to request clarification or add additional context in comments.

2 Comments

How do you unlink the socket? There is no information on how to do it in the docs. EDIT: Ah, by deleting the file. fs.unlinkSync(ipcPath)
@JoakimL.Christiansen, yeap, fs.unlinkSync() is one way to do it. On systems that support unix sockets, the unlink command I referenced in the post exists and is another way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.