ataraxia
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 partially connected mesh network.
Features
- 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
Example with TCP transport
import { Network, AnonymousAuth } from 'ataraxia';
import { TCPTransport, TCPPeerMDNSDiscovery } from 'ataraxia-tcp';
const net = new Network({
name: 'name-of-your-app-or-network',
authentication: [
new AnonymousAuth()
]
});
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.data, 'from', msg.source.id);
});
await net.start();
Example with machine-local transport and TCP transport
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';
const net = new Network({
name: 'name-of-your-app-or-network',
authentication: [
new AnonymousAuth()
]
});
const local = new MachineLocalTransport();
local.onLeader(() => {
net.addTransport(new TCPTransport({
discovery: new TCPPeerMDNSDiscovery()
}));
});
net.addTransport(local);
await net.start();
API
Network
new Network(options)
- create a new network using the given options. Options may be:
name
- Required. The name of the network, should be short and describe the app or library.endpoint
- Request that the local node is an endpoint that should not perform routing.
start()
- start the network and its transportsstop()
- stop the network and its transportsaddTransport(transport)
- add a transport that should be usedonNodeAvailable(node => ...)
- a node has been found and messages can now be sent and received to/from itonNodeUnavailable(node => ...)
- a node is no longer availableonMessage((message: Message) => ...)
- a message has been received from a node
Node
id
- get the id of the nodeonUnavailable(() => ...)
- node is no longer availablesend(type, data)
- send a message of the given type with the specified data to the nodeonMessage((message: Message) => ...)
- a message has been received from this node
Message
source: Node
- the node that sent the messagetype: string
- the type of the messagedata: any
- the data of the message