
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
@nats-io/services
Advanced tools
services library - this library implements all the base functionality for NATS services for javascript clients
For a quick overview of the libraries and how to install them, see runtimes.md.
The Services module introduces a higher-level API for implementing services with NATS. NATS has always been a strong technology on which to build services, as they are easy to write, are location and DNS independent and can be scaled up or down by simply adding or removing instances of the service.
The services module further streamlines NATS services development by providing observability and standardization. The Service Framework allows your services to be discovered, queried for status and schema information without additional work.
To create services using the services module simply install this library and
create a new Svc(nc)
.
const svc = new Svc(nc);
const service = await svc.add({
name: "max",
version: "0.0.1",
description: "returns max number in a request",
});
// add an endpoint listening on "max"
const max = service.addEndpoint("max", (err, msg) => {
msg?.respond();
});
If you omit the handler, the service is actually an iterator for service messages. To process messages incoming to the service:
for await (const r of max) {
r.respond();
}
For those paying attention, this looks suspiciously like a regular subscription. And it is. The only difference is that the service collects additional metadata that allows the service framework to provide some monitoring and discovery for free.
To invoke the service, it is a simple NATS request:
const response = await nc.request("max", JSON.stringify([1, 2, 3]));
When the service started, the framework automatically assigned it an unique
ID
. The name
and ID
identify particular instance of the service. If you
start a second instance, that instance will also have the same name
but will
sport a different ID
.
To discover services that are running, create a monitoring client:
const m = svc.client();
// you can ping, request info, and stats information.
// All the operations return iterators describing the services found.
for await (const s of await m.ping()) {
console.log(s.id);
}
await m.stats();
await m.info();
Additional filtering is possible, and they are valid for all the operations:
// get the stats services that have the name "max"
await m.stats("max");
// or target a specific instance:
await m.stats("max", id);
For a more elaborate first example see: simple example here
More complex services will have more than one endpoint. For example a calculator
service may have endpoints for sum
, average
, max
, etc. This type of
service is also possible with the service api.
You can create the service much like before. In this case, you don't need the endpoint (yet!):
const calc = await nc.services.add({
name: "calc",
version: "0.0.1",
description: "example calculator service",
});
One of the simplifications for service it that it helps build consistent subject hierarchy for your services. To create a subject hierarchy, you add a group.
const g = calc.addGroup("calc");
The group is a prefix subject where you can add endpoints. The name can be anything that is a valid subject prefix.
const sums = g.addEndpoint("sum");
(async () => {
for await (const m of sums) {
// decode the message payload into an array of numbers
const numbers = m.json<number[]>();
// add them together
const s = numbers.reduce((sum, v) => {
return sum + v;
});
// return a number
m.respond(JSON.stringify(s));
}
})();
addEndpoint()
takes a name, and an optional handler (it can also take a set of
options). The name
must be a simple name. This means no dots, wildcards, etc.
name
is then appended to the group where it is added, forming the subject
where the endpoint listens.
In the above case, the sum
endpoint is listening for requests on calc.sum
.
For those paying attention, you can specify a callback much like in the first example, if you don't, the return value of the add endpoint is an iterator.
For a complete example see: multiple endpoints
FAQs
services library - this library implements all the base functionality for NATS services for javascript clients
We found that @nats-io/services demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.