Options
All
  • Public
  • Public/Protected
  • All
Menu

Module ataraxia-services

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.

import { Services, ServiceContract, stringType } from 'ataraxia-services';

const net = ... // setup network with at least one transport

const services = new Services(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
await net.join();

// Join the services on top of the network
await services.join();

// Use contracts to describe services
const EchoService = new ServiceContract()
.defineMethod('echo', {
returnType: stringType,
parameters: [
{
name: 'message',
type: stringType
}
]
});

// Easily register and expose services to other nodes
services.register('echo', EchoService.implement({
echo(message) {
return Promise.resolve(message);
}
}));

// Consume a service registered anywhere, local or remote
const echoService = services.get('echo');
if(echoService.available) {
// Call methods
await echoService.call('echo', 'Hello world');

// Or create a proxy for a cleaner API
const proxied = echoService.as(EchoService);
await proxied.echo('Hello world');
}

Index

References

AsyncEvent

Re-exports AsyncEvent

AsyncSubscribable

Re-exports AsyncSubscribable

BasicValue

Re-exports BasicValue

DataType

Re-exports DataType

Listener

Re-exports Listener

ServiceContract

Re-exports ServiceContract

ServiceEventContract

Re-exports ServiceEventContract

ServiceEventDescription

Re-exports ServiceEventDescription

ServiceMethodContract

Re-exports ServiceMethodContract

ServiceMethodDescription

Re-exports ServiceMethodDescription

ServiceParamDescription

Re-exports ServiceParamDescription

ServiceParameterContract

Re-exports ServiceParameterContract

arrayType

Re-exports arrayType

booleanType

Re-exports booleanType

numberType

Re-exports numberType

serviceContract

Re-exports serviceContract

stringType

Re-exports stringType

voidType

Re-exports voidType

Type aliases

LocalService

LocalService<T>: T | ((handle: ServiceHandle) => T) | (new (handle: ServiceHandle) => T)

Type definition for local services. Supports three cases:

  • Already constructed instance
  • Function that takes a ServiceHandle and returns instance
  • Constructor that takes a ServiceHandle

Type parameters

  • T

Generated using TypeDoc