Skip to content

P2P messaging over mesh networks for NodeJS

License

Notifications You must be signed in to change notification settings

syncwaretechnologies/ataraxia

 
 

Repository files navigation

Ataraxia

npm version

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:

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';

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

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';

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

Support for services

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';
  }
});

About

P2P messaging over mesh networks for NodeJS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.7%
  • JavaScript 0.3%