Skip to content

Simple inter-process communication using Unix domain socket. Proof of concept wrapper with an example.

Notifications You must be signed in to change notification settings

donvercety/IPCUnixSocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simple inter-process communication using Unix domain socket. Proof of concept wrapper with an example.

Examples commands:

  • $ MODE=server node example.js initialize server
  • $ MODE=client node example.js initialize client

Include the helper library:

const IPCUnixSocket = require('./lib/IPCUnixSocket');

Example Server:

const socketLocation = '/tmp/unix.sock';
const unixSocket = new IPCUnixSocket(socketLocation);
const server = unixSocket.startServer();

server.on('connection', stream => {
	console.log(`stream id:${stream.id}`);

	stream.on('data', pkg => {
		pkg = pkg.toString('utf8');
		console.log(`Received: ${pkg}`);
	});
});

// example: ping message every 30 seconds
setInterval(() => {
	let clients = Object.keys(unixSocket.connections);
	
	for (let client of clients) {
		let connection = unixSocket.connections[client];
		
		if (connection) {
			connection.write('ping')
			console.log(`Send to ${connection.id} ping`);
		}
	}

}, 30 * 1000);

Example Client:

const socketLocation = '/tmp/unix.sock';
const unixSocket = new IPCUnixSocket(socketLocation);
const client = unixSocket.startClient();

client.on('connect', () => {
	console.log('Connected');
});

client.on('data', pkg => {
	pkg = pkg.toString('utf8');
	console.log(`Received: ${pkg}`);

	if (pkg === 'ping') {
		console.log(`Send: pong`);
		client.write('pong');
	}
});

client.on('error', err => {
	console.log(err.stack);
	client.end();
});

About

Simple inter-process communication using Unix domain socket. Proof of concept wrapper with an example.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published