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?
The traditional UNIX system calls are blocking. For example: accept() blocks the caller until a connection is present. If no messages space is available at the socket to hold the message to be transmitted, then send() normally blocks.
// make a connection with the user from server side io. on('connection', (socket)=>{ console. log('New user connected'); }); Similarly, from the client-side, we need to add a script file and then make a connection to a server through which users send data to a server.
A UNIX socket, AKA Unix Domain Socket, is an inter-process communication mechanism that allows bidirectional data exchange between processes running on the same machine. IP sockets (especially TCP/IP sockets) are a mechanism allowing communication between processes over the network.
Unix domain sockets are often twice as fast as a TCP socket when both peers are on the same host. The Unix domain protocols are not an actual protocol suite, but a way of performing client/server communication on a single host using the same API that is used for clients and servers on different hosts.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With