Mesh networking with peer-to-peer messaging for NodeJS and the browser. Ataraxia connects different instances together and allows messages to be passed between these instances. Some instances may act as routers for other instances to create a mesh network.
Ataraxia is split into several projects:
- ataraxia is the main library that provides the messaging functionality
- ataraxia-local provides a machine-local transport
- ataraxia-tcp provides a TCP-based transport with local network discovery
- ataraxia-ws-client provides a websocket client
- ataraxia-ws-server provides a websocket server
- ataraxia-services provides easy-to-use services with RPC and events
- Instances can send and receive messages from other instances
- Partially connected mesh network, messages will be routed to their target
- Authentication support, anonymous and shared secret authentication available in core
- RPC support via ataraxia-services that lets you call methods and receive events from services registered anywhere in the network
- Support for different transports
- ataraxia-local provides a machine-local transport
- ataraxia-tcp provides a TCP-based transport with customizable discovery of peers and TLS encryption
- ataraxia-ws-client and ataraxia-ws-server for websockets
import { Network, AnonymousAuth } from 'ataraxia';
import { TCPTransport, TCPPeerMDNSDiscovery } from 'ataraxia-tcp';
// Setup a network with anonymous authentication
const net = new Network({
name: 'name-of-your-app-or-network',
authentication: [
new AnonymousAuth()
]
});
// Setup a TCP transport that will discover other peers on the same network using mDNS
net.addTransport(new TCPTransport({
discovery: new TCPPeerMDNSDiscovery()
}));
net.onNodeAvailable(node => {
console.log('A new node is available:', node.id);
node.send('hello');
});
net.onMessage(msg => {
console.log('A message was received', msg.type, 'with data', msg.payload, 'from', msg.source.id);
});
net.start()
.then(...)
.catch(...);
This example creates a network where instances on the same machine connect to each other locally first and then elects one instance to handle connections to other machines on the same network.
import { Network, AnonymousAuth } from 'ataraxia';
import { TCPTransport, TCPPeerMDNSDiscovery } from 'ataraxia-tcp';
import { MachineLocalTransport } from 'ataraxia-local';
// Setup a network with anonymous authentication
const net = new Network({
name: 'name-of-your-app-or-network',
authentication: [
new AnonymousAuth()
]
});
const local = new MachineLocalTransport();
local.onLeader(() => {
/*
* The leader event is emitted when this instance becomes the leader
* of the machine-local network. This instance will now handle
* connections to other machines in the network.
*/
net.addTransport(new TCPTransport({
discovery: new TCPPeerMDNSDiscovery()
}));
});
net.addTransport(local);
net.start()
.then(...)
.catch(...);
Services are supported via ataraxia-services
, where objects can be registered
and functions on them called remotely:
import { Services } from 'ataraxia-services';
const net = ... // setup network with at least one transport
const services = new Services(net);
services.onAvailable(service => console.log(service.id, 'is now available'));
services.onUnavailable(service => console.log(service.id, 'is no longer available'));
// Start the network
await net.start();
// Start the services on top of the network
await services.start();
// Register a service as a plain object
const handle = services.register({
id: 'service-id',
hello() {
return 'Hello world';
}
});