Code Monkey home page Code Monkey logo

unix-dgram-socket's Introduction

unix-dgram-socket

Node compatibility

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.

Maintainability Test Coverage Codacy Badge

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();

// Call on error
socket.on('error', (error: any) => {
    console.log(error);
});

// Call when new message is received
socket.on('message', (message: Buffer, info: any) => {
    console.log(message.toString(UnixDgramSocket.payloadEncoding));
    console.log(info);
});

// Call when socket is bind to path
socket.on('listening', (path: string) => {
    console.log(`socket listening on path: ${path}`);
});

// Bind socket to filesystem path
socket.bind("/tmp/socket1.sock");

Sending messages

import {UnixDgramSocket} from "unix-dgram-socket";

const socket = new UnixDgramSocket();

// Call on error
socket.on('error', (error: any) => {
    console.log(error);
});

// Call when new message is received
socket.on('message', (message: Buffer, info: any) => {
    console.log(message.toString(UnixDgramSocket.payloadEncoding));
    console.log(info);
});

// Call on successful connect
socket.on('connect', (path: string) => {
    console.log(`socket connected to path: ${path}`);
});

// Call when socket is bind to path
socket.on('listening', (path: string) => {
    console.log(`socket listening on path: ${path}`);
});

socket.send("Special inter-process delivery!", "/tmp/socket1.sock");

// Dgram socket is connection-less so call connect only set default destination path and can be called many times
socket.connect("/tmp/socket1.sock");

// Send can be called without path if 'connect' was called before
socket.send("I will be send to default path, set by connect!");

// CLose socket to prevent further communication
socket.close();

Operating with abstract namespace path

Abstract namespace path can be passed by starting path string from null byte ('\0') or "@" character.

// Bind socket to abstract path
socket.bind("@/abstract/path/socket1.sock");

// Send data to abstract namespace path
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

unix-dgram-socket's People

Contributors

dependabot[bot] avatar redneb avatar ulwanski avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

unix-dgram-socket's Issues

Build error - Node v12?

Trying to install this module from NPM, I get a bunch of compile errors, eg.:
../src/unix_dgram.cc:358:28: error: no matching function for call to ‘v8::Value::Int32Value()’

../src/unix_dgram.cc:429:63: error: no matching function for call to ‘v8::FunctionTemplate::GetFunction()’

My knowledge on Nan is limited, but it must be related to V8 deprecating a lot of API calls.

I think the similar library node-unix-dgram got most of it covered in this commit:
bnoordhuis/node-unix-dgram@0ae2a61#diff-8e3ec9d4b59eaf953bbd7fe258cb5700

Improve error handling

socket.send not throw error

All kinds of errors are handled by socket.on('error', callback).

And this leads to the following problem.

let connectionInfo = {};
socket.on('message', (message, info) => {
    let path = info.remoteSocket;
    if (!connectionInfo[info.remoteSocket]) {
        connectionInfo[info.remoteSocket] = { path };
    }
    let currentInfo = connectionInfo[path];
    currentInfo.id = setInterval(_ => {
        socket.send(data, info.remoteSocket);
    }, 1000);
});

socket.on('error', (error) => {
	// can't stop setInterval callback
});

I suggest two ways.

  • Improvement 1 (socket.send throw error)
let connectionInfo = {};
socket.on('message', (message, info) => {
    let path = info.remoteSocket;
    if (!connectionInfo[info.remoteSocket]) {
        connectionInfo[info.remoteSocket] = { path };
    }
    let currentInfo = connectionInfo[path];
    currentInfo.id = setInterval(_ => {
        try {
            socket.send(data, info.remoteSocket);
        } catch (e) {
            clearInterval(currentInfo.id);
        }
    }, 1000);
});
  • Improvement 2 (return error object with remoteSocket)
socket.on('error', (error) => {
	if(error.syscall == 'send'){
		clearInterval(connectionInfo[error.remoteSocket].id);
	}
});

:)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.