Skip to content

Repository files navigation

netlinkwrapper

NetLinkWrapper allows access to TCP and UDP sockets via synchronous calls safety across platforms.

This is a simple node-gyp module wrapper for the C++ library NetLink Sockets. Meaning this is a simple wrapper for a simple TCP/UDP socket. Also, and perhaps most importantly, this is an implementation that only works synchronously, as opposed to Node's asynchronous paradigm.

Purpose

Obviously node's net/[dgram] modules are far more suited for most TCP/UDP communications than this module. However, those modules can only work asynchronously, as is Node's design. To contrast, that is the opposite of the intention of this module. If you are in the very odd situation where you need synchronous usage of sockets across platforms, then this module may suit you.

How to use

As with most modules, use npm or your preferred package manager to install it for you.

npm install netlinkwrapper

Notenode-gyp is a dependency. Ensure your setup can build C++ modules. Follow their documentation for installing the appropriate C++ build tools based on your environment.

Examples

TCP

const{ SocketClientTCP, SocketServerTCP }=require('netlinkwrapper');constport=33333;constserver=newSocketServerTCP(port);constclient=newSocketClientTCP(port,'localhost');constserverSends='hello world!';constserversClient=server.accept();serversClient.send(serverSends);constclientReceived=client.receive();constidentical=serverSends===clientReceived.toString();// should be trueconsole.log('the two strings are identical?',identical);client.disconnect();server.disconnect();

UDP

const{ SocketUDP }=require('netlinkwrapper');constportA=54321;constportB=12345;constsocketA=newSocketUDP(portA,'localhost');constsocketB=newSocketUDP(portB,'localhost');socketA.sendTo('localhost',portB,'Hello from socketA');constgot=socketB.receiveFrom();constidentical=got.port===portA;console.log('identical?',identical);// should be: true// should be: 'got: 'Hello from socketA' from localhost:54321'console.log(`got: '${got.data.toString()}' from ${got.host}:${got.port}`);socketA.disconnect();socketB.disconnect();

Other Notes

Due to the connection-less nature of UDP, the same constructor can be used to make a "client" or "server" esc UDP socket.

After calling disconnect on a socket it is considered "destroyed", and cannot be re-used. In addition, attempting to call any functions off it will result in Errors being thrown.

TypeScript types are included in this package. They are highly encouraged to use. Attempting to do anything outside the scope of the included types will probably result in Errors being thrown.

Docs

Official documentation can be found online at GitHub.

Alternatives

If you are looking for similar functionality, but without the node-gyp dependency I have made a similar (but much slower) module, SyncSocket.

About

A node-gyp netLink wrapper

Resources

Stars

17 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages