Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Network<MessageTypes>

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.

const net = new Network({
name: 'name-of-network',

transports: [
new MachineLocalNetwork()
]
});

await net.join();

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.

const group = new NamedGroup(net, 'name-of-group');

// Groups need to be joined
await group.join();

// Broadcast to the known members
await group.broadcast('typeOfMessage', dataOfMessage);

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:

interface EchoMessages {
'namespace:echo': { message: string };
'namespace:echo-reply': { reply: string };
}

An group can then be typed via:

const group: Group<EchoMessages> = new NamedGroup<EchoMessage>(net, 'echo');

This will help TypeScript validate messages that are sent:

// TypeScript will allow this
group.broadcast('namespace:echo', { message: 'Test' });

// TypeScript will not allow these
group.broadcast('namespace:echo', { msg: 'Test' });
group.broadcast('namespace:e', { message: 'Test' });

The same is true for listeners:

group.onMessage(msg => {
if(msg.type === 'namespace:echo') {
// In here msg.data will be of the type { message: string }
const data = msg.data;
msg.source.send('namespace:echo-reply', { reply: data.message })
.catch(errorHandler);
} else if(msg.type === 'namespace:echo-reply') {
// msg.data will be { reply: string }
} else {
// No message of this type
}
});

Type parameters

  • MessageTypes: object = any

Hierarchy

  • Network

Implements

Index

Constructors

constructor

  • 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 network
    • endpoint - boolean indicating if this instance is an endpoint and wants to avoid routing.
    • transports - array of transports that the network should start

    Type parameters

    • MessageTypes: object = any

    Parameters

    Returns Network<MessageTypes>

Properties

Readonly endpoint

endpoint: boolean

If this node is connecting to the network as an endpoint.

Readonly name

name: string

The name of the network.

Readonly networkIdBinary

networkIdBinary: ArrayBuffer

The identifier this node has when connecting to the network.

Accessors

networkId

  • get networkId(): string
  • The identifier this local node has, this is the name other nodes see us as.

    Returns string

    network identifier as string

nodes

  • get nodes(): Node<any>[]

onMessage

  • get onMessage(): Subscribable<this, [message: MessageUnion<MessageTypes>]>

onNodeAvailable

  • get onNodeAvailable(): Subscribable<this, [node: Node<MessageTypes>]>

onNodeUnavailable

  • get onNodeUnavailable(): Subscribable<this, [node: Node<MessageTypes>]>

Methods

addTransport

  • Add a transport to this network. If the network is started the transport will also be started.

    Parameters

    • transport: Transport
      • instance of transport to add

    Returns void

broadcast

  • broadcast<T>(type: T, data: MessageData<MessageTypes, T>): Promise<void>
  • Broadcast a message to all nodes.

    Type parameters

    • T: string

    Parameters

    • type: T
      • the type of message to send
    • data: MessageData<MessageTypes, T>
      • the data of the message

    Returns Promise<void>

    promise that resolves when the message has been broadcast to all known nodes

getService

  • getService<T>(factory: new (handle: Network<any>) => T): T
  • Get a service as a singleton. This is useful for starting a single instance of shared services.

    Type parameters

    • T

    Parameters

    • factory: new (handle: Network<any>) => T
      • constructor that takes instance of network

    Returns T

    instance of factory

join

  • join(): Promise<void>
  • Join the network by starting a server and then looking for peers.

    Returns Promise<void>

    promise that resolves when the network is started, the value will represent if the network was actually started or not.

leave

  • leave(): Promise<void>

Generated using TypeDoc