Create a new network. A network must be provided a name
which is a
short string used that transports may use to discover peers. Such a
short name is usually something like app-name
or known-network-name
.
These options are available:
name
- the name of the networkendpoint
- boolean indicating if this instance is an endpoint and
wants to avoid routing.transports
- array of transports that the network should startIf this node is connecting to the network as an endpoint.
The name of the network.
The identifier this node has when connecting to the network.
The identifier this local node has, this is the name other nodes see us as.
network identifier as string
Get a snapshot of nodes that can be currently seen in the network.
array of nodes
Event emitted when a message is received from any node on the network.
subscribable function
Add a transport to this network. If the network is started the transport will also be started.
Broadcast a message to all nodes.
promise that resolves when the message has been broadcast to all known nodes
Join the network by starting a server and then looking for peers.
promise that resolves when the network is started, the value will represent if the network was actually started or not.
Leave the currently joined network.
promise that resolves when the network is stopped
Generated using TypeDoc
Network of nodes. The network is the main class in Ataraxia and uses one or more transports to connect to peers and discover nodes in the network.
Networks are required to have a name which represents a short name that describes the network. Transports can use this name to automatically find peers with the same network name.
Networks can be joined and left as needed. The same app is encouraged to only join the network once and then share an instance of
Network
as needed.Nodes of the network
When a network is joined this instance will start emitting events about what nodes are available on the network. It is recommended to use onNodeAvailable and onNodeUnavailable to keep track of what nodes the instance can communicate with.
It's possible to iterate over a snapshot of nodes using nodes.
Sending and receiving messages
Messaging in Ataraxia does not guarantee delivery, messages may or may not reach their intended targets.
The onMessage event can be used to listen to events from any node, which is recommended to do when building something that deals with many nodes. If you're only interested in messages from a single node, Node.onMessage can be used instead.
To send a message to a single node use Node.send.
It is possible to broadcast a message to all the known nodes via broadcast, but as with regular messages no delivery is guaranteed and large broadcasts are discouraged.
Groups
Groups are a way to create named area of the network that nodes can join and leave as needed. Broadcasting a message on an group will only send it to known members of the groups.
Typing of messages
The network and groups can be typed when using TypeScript.
The types are defined as an interface with the keys representing the message types tied to the type of message:
An group can then be typed via:
This will help TypeScript validate messages that are sent:
The same is true for listeners: