unix-dgram-socket
Connection-less, reliable unix datagram socket implementation with abstract namespace support for local interprocess communication in Node.JS application.
UNIX domain sockets can be either unnamed, or bound to a filesystem pathname (marked as being of type socket).
Linux also supports an abstract namespace which is independent of the filesystem.
Installation
npm i unix-dgram-socket --save
Package C++ addons will be compiled during installation.
Examples
Open socket
import {UnixDgramSocket} from "unix-dgram-socket";
const socket = new UnixDgramSocket();
socket.on('error', (error: any) => {
console.log(error);
});
socket.on('message', (message: Buffer, info: any) => {
console.log(message.toString(UnixDgramSocket.payloadEncoding));
console.log(info);
});
socket.on('listening', (path: string) => {
console.log(`socket listening on path: ${path}`);
});
socket.bind("/tmp/socket1.sock");
Sending messages
import {UnixDgramSocket} from "unix-dgram-socket";
const socket = new UnixDgramSocket();
socket.on('error', (error: any) => {
console.log(error);
});
socket.on('message', (message: Buffer, info: any) => {
console.log(message.toString(UnixDgramSocket.payloadEncoding));
console.log(info);
});
socket.on('connect', (path: string) => {
console.log(`socket connected to path: ${path}`);
});
socket.on('listening', (path: string) => {
console.log(`socket listening on path: ${path}`);
});
socket.send("Special inter-process delivery!", "/tmp/socket1.sock");
socket.connect("/tmp/socket1.sock");
socket.send("I will be send to default path, set by connect!");
socket.close();
Operating with abstract namespace path
Abstract namespace path can be passed by starting path string from null byte ('\0') or "@" character.
socket.bind("@/abstract/path/socket1.sock");
socket.send("Special inter-process delivery!", "@/abstract/path/socket1.sock");
Additional information
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
GPL-3.0
References