Microserv
JSON-RPC over websocket with multicast service discovery with a browser compatible client
Installation
npm install --save microserv
Usage
Service A
const Server Service = const server = 'my-app' port: 3000 const msgService = 'message' msgService serverserverserver
Service B
const http = const Server Service = const server = 'my-app' port: 3001 const mathService = 'math' mathService server // Wait for the message service to be readyserver serverserver
Client
const Client = const client = client clientclient
Server
const server = 'my-app' port: 3001
new Server(namespace[,options]) -> Client
Instantiate a client
Parameters:
address
{String}: The service namespace. Only services in the same namespace can connect to each other.options
{Object}: Websocket optionsport
{Number}: Websocket portinterval
{Number}: How often to announce the service (in ms). Defaults to5000
.sever
{http.Server|https.server}: A server to use as the websocket server. If set, will overrideport
.secure
{Boolean}: Use secure websockets. Defaults tofalse
.serviceTransform
{Function}: A function to transform the data returned from an rpc call. Defaults to noop.authorization
{Any}: Credentials to pass to Server when connectingauthorizeClient
{Function}: A sync or async function to authorize a Client. Function is passed the Client'sauthorization
credentialsdns
{Object}: dns-discovery options
server.addService(service) -> Server
Add a service to the server, and announce the service to connected peers
Parameters:
service
{Service}: A Service instance
server.need(...services) -> Promise
List required services. Resolves with each of the required services of type ClientService
Parameters:
services
{String}: Service names
server.listen() -> Server
Begin listening for peers
server.announce() -> Server
Announce the service on the network. Will reannounce the service at the interval provided via opts
.
Event: 'error'
Emitted when there is an error on the server
Event: 'connection_reset'
Emitted when a connected socket is reset
Service
const service = 'my-service'
new Service(name) -> Service
Instantiate a new Service
Parameters:
name
{String}: The service name that will be announced to peers
service.register(method, cb[, resultType]) -> Service
Register an rpc method with the service
Parameters:
method
{String}: The name of the methodcb
{Function}: The function to invoke when the rpc method is calledresultType
{String}: The type of data returned by the method. Defaults totypeof result
service.emit(name, data)
Emits an event via the websocket
Parameters:
name
{String}: The event namedata
{*}: The event data
ClientService
Returned from server.need()
. This should not be instantiated direclty.
clientService.subscribe(name, cb)
Subscribe to a service event
Parameters:
name
{String}: The name of the eventcb
{Function}: The event callback
clientService.service_method(*) -> Object
Each method registered with the service is exposed a method of the client service. Returns and object with data
and type
properties.
Event: 'close'
Emitted when the connection to the service is lost
Event: 'reopen'
Emitted when the connection to a lost service is reestablished
Client
const client =
new Client([opts, rpcOpts]) -> Client
Instantiate a new Client
Parameters:
opts
{Object}: OptionsserviceTransform
{Function}: A function to transform the data returned from an rpc call. Defaults to noop.authorization
{Any}: Credentials to pass to Server when connecting
rpcOpts
{Object}: Options for rpc-websockets
client.connect(connection) -> Websocket
Connect to a specific websocket server
Parameters:
connection
{String}: The server connection string (eg. ws://localhost:3001)
client.need(...service) -> Promise
List required services. Resolves with each of the required services of type ClientService
Parameters:
services
{String}: Service names
Event: 'close'
Emitted when the connection to a service is closed
Event: 'error'
Event: 'unauthorized'
Emitted when the client fails authorization
Tests
npm run test