@service-broker/client-node
v2.2.0
Published
Node ESM client library for interacting with a service broker
Downloads
7
Readme
@service-broker/client-node
Node ESM client library for interacting with a service broker
Install
npm install @service-broker/client-nodeConnect
const sb = new ServiceBroker({
// websocket URL of the service broker
url: string,
// if the service-broker requires providers to authenticate when advertising
authToken?: string,
// callback for when connection is established (or re-established)
onConnect?: () => void,
// disable auto-reconnect function (upon connect failure or connection loss)
disableAutoReconnect?: boolean,
// whether to send websocket pings periodically
keepAliveIntervalSeconds?: number,
// logger for info and errors
logger?: Logger,
})Advertise
sb.advertise(
service: {name: string, capabilities?: string[], priority?: number},
handler: (req: Message) => Message|Promise<Message>
)A service provider calls this method to advertise to the broker the service(s) it provides. For explanation of parameters, see service broker.
Request
sb.request(
service: {name: string, capabilities?: string[]},
req: Message,
timeout?: number
)
: Promise<Message>A client calls this method to request some service. The broker will select a qualified provider based on service name and capabilities. The parameter req contains the actual message that'll be delivered to the service provider (see handler in Advertise). The returned promise contains the response from the provider.
RequestTo
sb.requestTo(
endpointId: string,
serviceName: string,
req: Message,
timeout?: number
)
: Promise<Message>Send a request directly to an endpoint.
Notify
sb.notify(
service: {name: string, capabilities?: string[]},
msg: Message
)A notification is a request without an id, and thus no response will be generated. Use a notification when you don't care about the result.
NotifyTo
sb.notifyTo(
endpointId: string,
serviceName: string,
msg: Message
)Send a notification directly to an endpoint.
Publish/Subscribe
sb.publish(
topic: string,
text: string
)
sb.subscribe(
topic: string,
handler: (text: string) => void
)SetServiceHandler
sb.setServiceHandler(
serviceName: string,
handler: (req: Message) => Message|Promise<Message>
)This installs a handler for a particular service without advertising the it to the broker. This is for when the client knows the provider's endpointId and can send the request directly to it (see RequestTo).
Status
sb.status(): Promise<any>Returns the service broker's status, which includes the list of services and service providers currently active.
WaitEndpoint
sb.waitEndpoint(endpointId: string): Promise<void>Returns a promise that resolves when the specified endpoint disconnects.
Shutdown
sb.shutdown()Close the connection to the service broker.
