constnet = ... // setup network with at least one transport
constservices = newServices(net);
services.onAvailable(service=>console.log(service.id, 'is now available')); services.onUnavailable(service=>console.log(service.id, 'is no longer available'));
// Join the network awaitnet.join();
// Join the services on top of the network awaitservices.join();
// Use contracts to describe services constEchoService = newServiceContract() .defineMethod('echo', { returnType:stringType, parameters: [ { name:'message', type:stringType } ] });
// Easily register and expose services to other nodes services.register('echo', EchoService.implement({ echo(message) { returnPromise.resolve(message); } }));
// Consume a service registered anywhere, local or remote constechoService = services.get('echo'); if(echoService.available) { // Call methods awaitechoService.call('echo', 'Hello world');
// Or create a proxy for a cleaner API constproxied = echoService.as(EchoService); awaitproxied.echo('Hello world'); }
Services with RPC and events, provides a service layer on top of a Network allowing nodes to register, discover and call services on the network.