@xaymar/datapath
DataPath is an IPC library for fast and predictable messaging between processes on the same machine made in C++11 (or newer). Designed from the ground up to care about the worst case it is able to outperform or match existing solutions such as boost interprocess, libuv, Mojo and others. It allows for packets from a few bytes all the way to many megabytes between one server and any number of clients. The exact implementation varies by platform and makes use of the best measured available systems.
These are the official bindings for DataPath
Features
- Low Latency, High Bandwidth. Heavily optimized for even the worst of the worst cases, achieving under 100µs even under near 100% system load for up to 4 KiB messages.
- Thread-safe by design. Messages are never interleaved and
send()can be used from any thread without blocking. - No fragmentation. Every
send()message arrives as exactly onemessage()event. - Cross-Platform by default. Windows, Linux, macOS supported by design.
- Access Control built in. Set
User,GroupandWorldaccess to exactly what you need. - One API. One Standard. The same API available on every platform and even in the official bindings.
- Near Zero Dependencies. Only the standard C++ library is required and a functional OS.
- Built to KISS. The API is kept as simple as possible so mistakes are much harder to make.
Node specific Features
- Built with Node API. One binary works on every Node.JS release from 18.x onwards. Electron may work too (untested).
- Interoperable with any DataPath client or server. The same C++ library is included directly and linked statically so no separate protocol.
- Typescript included. No need to guess, Typescript has you covered.
Node specific Differences
- Servers and Sockets keep Node.JS alive. Event driven apps don't need a separate main loop and can enjoy the same behavior as
net.Socket- or they can opt out withunref(). - Handlers run on the JavaScript thread. Unlike C++ where message handlers can be on any thread the JavaScript binding runs everything on the JavaScript thread instead. Messages from
send()are queued up and the event loop is woken up so that bursts of messages act as one long time slice for the JavaScript handlers. Runnpm run benchon your machine for numbers that matter.
Usage
npm install @xaymar/datapath
const datapath = require('@xaymar/datapath');
// or: import { host, connect } from '@xaymar/datapath';
// Server: echo every message.
const server = datapath.host('my-service', (socket) => {
socket.on('message', (data) => socket.send(data));
socket.on('close', (reason) => console.log('client left:', reason));
});
// Client.
const socket = await datapath.connect('my-service');
socket.on('message', (data) => console.log(data.toString()));
socket.send('hello'); // string (UTF-8), Buffer, any TypedArray, DataView or ArrayBuffer
socket.sendv(['he', 'llo']); // one message from several parts, without joining them first
socket.close();
server.close();
API
host(path, [options], [onConnection]) → Serverhosts a server. Throws aDatapathErrorsuch asAddressInUse.connect(path, [options]) → Promise<Socket>connects and resolves after the handshake. Rejects with aDatapathErrorsuch asNotFoundorTimedOut.Serveremits'connection' (socket)and'close'.close()stops accepting; connected sockets stay open. A client accepted while no'connection'listener is attached is closed right away.Socketemits'message' (Buffer)and'close' (reason, hadError), which fires exactly once. Its methods aresend(data)andsendv(parts), which returnfalsewhen the send queue is full (WouldBlock) and throwTooLargeorClosed, plusclose(),ref()andunref(). Its properties areconnectedandmaxMessageSize.new Context(options)creates separate I/O threads with their own tuning:spin,ioThreads,priority,idealCore,mmcss,alertable,yield,pairPeer. Pass it asoptions.context. Without one, everything uses the process-wide default context.- Options map 1:1 to the C++
datapath::optionsin camelCase:maxMessageSize,maxQueuedBytes,receiveBuffer,connectTimeout(ms),handshakeTimeout(ms, host only),permissions('user','group','world'),backlog,maxClients,pipeBuffer,sharedMemory(defaulttrue),sharedMemorySize. DatapathErrorhascode(thedatapath::errorname, e.g.'NotFound') anderrno(its number).codesmaps names to numbers.
License
BSD 3-Clause License, see LICENSE.