npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nats-io/services

v3.0.0-20

Published

services library - this library implements all the base functionality for NATS services for javascript clients

Downloads

1,586

Readme

Services

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).

Creating a Service

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", JSONCodec().encode([1, 2, 3]));

Discovery and Monitoring

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

Multiple Endpoints

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 = JSONCodec<number[]>().decode(m.data);
    // add them together
    const s = numbers.reduce((sum, v) => {
      return sum + v;
    });
    // return a number
    m.respond(JSONCodec().encode(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