ataraxia-tcp
v0.12.0
Published
TCP transport for Ataraxia P2P messaging
Downloads
35
Readme
ataraxia-tcp
TCP transport for Ataraxia. This transport can discover and automatically connect to other peers using a discovery. A discovery is available based on mDNS and DNS-SD that automatically connects to other instances on the same local network.
Installation
npm install ataraxia-tcp
Simple usage
Create a TCP transport that will bind to a random free port and announce its availability on the local network:
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',
transports: [
new TCPTransport({
// Discover other peers on the same physical network
discovery: new TCPPeerMDNSDiscovery(),
// Use anonymous authentication
authentication: [
new AnonymousAuth()
]
})
]
});
// Join the network
await net.join();
Well-known ports and manual peers
In some cases you want to run a network that can be connected to from outside your local network. In that case its possible to define a port that the TCP transport will listen to:
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',
transports: [
new TCPTransport({
// A well known port - define your own or even better use a config file,
port: 30000,
// Use anonymous authentication
authentication: [
new AnonymousAuth()
]
})
]
});
// Join the network
await net.join();
Another instance can then connect to that specific port:
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'
});
const tcp = new TCPTransport({
authentication: [
new AnonymousAuth()
]
}):
// Add a manual addressing to the peer
tcp.addManualPeer({
host: 'address-to-server',
port: 30000
});
net.addTransport(tcp);
// Join the network
await net.join();
Ataraxia will attempt to connect to the manually added peers and will attempt to keep the connection available.
API
TCPTransport
new TCPTransport(options)
Create a new instance of this transport.
options
port?: number
, port number that the server should bind to. Leave undefined for automatic assignment.discovery?: TCPPeerDiscovery
, discovery instance used to discover and announce availability to other peers.authentication: AuthProvider[]
, array of authentication providers that this transport will use.
port: number
Port number bound to, will be
0
if no server was boundaddManualPeer(hostAndPort: { host: string, port: number }): void
Add a peer that should be manually connected to.
TCPPeerMDNSDiscovery
new TCPPeerMDNSDiscovery()
Create a discovery instance that will use mDNS and DNS-SD on the local physical network.