Quantcast
Channel: Recent Gists from badsyntax
Viewing all articles
Browse latest Browse all 31

Super simple Node.js IPC with Unix domain sockets

$
0
0
node-ipc.js
const net = require('net');
const socketPath = '/tmp/my.unix.sock';
const server = net
.createServer()
.on('connection', (stream) => {
console.log('Server: client connected');
stream.setEncoding('utf-8');
stream.on('end', () => {
console.log('Server: client disconnected');
});
stream.on('data', (msg) => {
console.log('Server: got client message:', msg);
if (msg === 'command') {
stream.write('command response');
}
});
})
.on('close', () => {
console.log('Server: shut down');
})
.listen(socketPath, () => {
console.log('Server bound to socket at path:', socketPath);
});
const client = net.createConnection(socketPath);
client
.setEncoding('utf-8')
.on('connect', () => {
console.log('Client: connected to server');
client.write('command');
client.end();
server.close();
})
.on('data', (data) => {
console.log('Client: got server message:', data);
})
.on('end', () => {
console.log('Client: disconnected');
})
.on('error', (data) => console.error(data));

Viewing all articles
Browse latest Browse all 31

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>