<|start_filename|>examples/ws-server.js<|end_filename|>
/**
 * This example starts a network named `example` and uses a WebSocket server
 * transport on port 7000. WebSocket clients can then connect to this server
 * via the example `ws-client.js`.
 *
 * It will log when new nodes are discovered and when messages are received.
 * In addition to this it sends a hello to nodes when they are seen and
 * broadcasts a counter every 5 seconds to all current nodes.
 */

const { Network, AnonymousAuth } = require('../packages/core');
const { WebSocketServerTransport } = require('../packages/ws-server');

const net = new Network({
	name: 'example',

	transports: [
		// Add the WebSocket transport auto-starting a server on port 7000
		new WebSocketServerTransport({
			port: 7000,
			authentication: [
				new AnonymousAuth()
			]
		})
	]
});

// Log when new nodes are available and send them a hello with the counter
net.onNodeAvailable(node => {
	console.log('Node', node, 'is now available');
});

net.onNodeUnavailable(node => {
	console.log('Node', node, 'is no longer available');
});

// Log when messages have been received
net.onMessage(msg => {
	console.log('Message was received:', 'type=', msg.type, 'data=', msg.data, 'source=', msg.source);
});

// Start the network
net.join()
	.then(() => {
		console.log('Network has been joined with id', net.networkId);

		// Start our helper
		return require('./helpers/counter')(net);
	})
	.catch(err => console.error(err));


<|start_filename|>examples/helpers/counter.js<|end_filename|>
const { NamedGroup } = require('../../packages/core');

module.exports = async function(net) {
	let counter = 0;

	const group = new NamedGroup(net, 'counter');
	group.onNodeAvailable(node => {
		node.send('hello', { counter: counter })
			.catch(e => console.log('Timed out sending hello'));
	})

	await group.join();

	// Increment and broadcast counter to all nodes every five seconds
	setInterval(() => {
		group.broadcast('counter', { current: ++counter });
	}, 5000);
}


<|start_filename|>tsconfig.json<|end_filename|>
{
	"extends": "./tsconfig.settings.json",
	"compilerOptions": {
		"module": "commonjs",

		"noEmit": true,

		"baseUrl": ".",
		"paths": {
			"ataraxia": [ "./packages/core/src" ],
			"ataraxia-*": [ "./packages/*/src" ]
		}
	},
	"include": [ "packages/*/src" ],
	"exclude": [ "**/node_modules/**", "**/dist/**" ],
	"typedocOptions": {
		"name": "Ataraxia",
		"entryPoints": [
			"packages/transport/src/index.ts",
			"packages/transport-streams/src/index.ts",

			"packages/local/src/index.ts",
			"packages/tcp/src/index.ts",
			"packages/hyperswarm/src/index.ts",
			"packages/ws-client/src/index.ts",
			"packages/ws-server/src/index.ts",

			"packages/core/src/index.ts",

			"packages/service-contracts/src/index.ts",
			"packages/services/src/index.ts"
		],
		"readme": "./docs/apidocs.md",
		"out": "apidocs",
		"excludePrivate": true
	}
}


<|start_filename|>examples/ws-server-tcp-bridge.js<|end_filename|>
/**
 * This example starts a network named `example` and uses a WebSocket server
 * transport on port 7000 and a TCP transports. This allows WebSocket clients
 * to connect to this server and send and receive messages from TCP-based nodes.
 *
 * It will log when new nodes are discovered.
 */

const { Network, AnonymousAuth } = require('../packages/core');
const { WebSocketServerTransport } = require('../packages/ws-server');
const { TCPTransport, TCPPeerMDNSDiscovery } = require('../packages/tcp');

const net = new Network({
	name: 'example',

	transports: [
		// Add the WebSocket transport auto-starting a server on port 7000
		new WebSocketServerTransport({
			port: 7000,

			authentication: [
				new AnonymousAuth()
			]
		}),

		// Add the TCP transport with mDNS discovery
		new TCPTransport({
			discovery: new TCPPeerMDNSDiscovery(),

			authentication: [
				new AnonymousAuth()
			]
		})
	]
});

// Log when new nodes are available and send them a hello with the counter
net.onNodeAvailable(node => {
	console.log('Node', node, 'is now available');
});

net.onNodeUnavailable(node => {
	console.log('Node', node, 'is no longer available');
});

// Start the network
net.join()
	.then(() => console.log('Network has been joined with id', net.networkId))
	.catch(err => console.error(err));
