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

fastify-nats-client

v4.1.0

Published

Fastify plugin for using NATS

Downloads

28

Readme

fastify-nats-client

NPM Version NPM Downloads Code Style license - APACHE-2.0

Fastify Plugin to use NATS Server to exchange messages via queues etc.

Under the hood nats.js library is used; the plugin has some options (to set in the register), some will be sent to the nats library for connection details etc.

Usage

Add it to you project with register and you are done!
You can access the nats Connection via fastify.nc. Note that even the NATS library is exposed at fastify.NATS but only as a convenience (to avoid refer to it directly as a Node.js library).

const fastify = require('fastify')

// register the plugin with some options, for example:
fastify.register(require('fastify-nats-client'), {
  natsOptions: { servers: 'nats://demo.nats.io:4222' }
})

fastify.listen({ port: 3000, host: 'localhost' }, (err, address) => {
  if (err) throw err
  console.log(`server listening on ${address}`)
})

and later

// get some NATS-related facilities
const nc = fastify.nc // get the NATS Connection with servers
const sc = fastify.NATS.StringCodec() // codec for a string message
const jc = fastify.NATS.JSONCodec() // codec for a JSON string message
// const subject = fastify.NATS.createInbox() // sample queue name
// publish/subscribe, example
nc.publish(queueName, sc.encode(msg)) // simple publisher for a string message
nc.publish(queueName, jc.encode(obj)) // simple publisher for a JSON message
nc.subscribe( ... ) // use an async iterator or a callback
// etc ...

In the example folder there is a simple server scripts that uses the plugin (inline but it's the same using it from npm registry).

As you can see, the NATS.js library is complex and with a lot of features, please refer to its documentation and sources for more info and examples.

Requirements

Fastify ^4.0.1 , Node.js 14 LTS (14.15.0) or later.

Note that plugin releases 3.x are for Fastify 3.x, 4.x are for Fastify 4.x, etc.

Sources

Source code is all inside main repo: fastify-nats-client.

Documentation generated from source code (library API): here.

Note

All the code here is based on the work done initially by its original author (mahmed8003 [email protected]), in the upstream repository fastify-nats, under the MIT license.

The plugin decorate Fastify and expose some functions:

  • NATS, a reference to the NATS library, but only as a convenience
  • nc, the NATS Connection to use; even if a little criptic, I used those names to better align with NATS.js sources and examples

Some plugin options are sent directly to NATS.js - NATS-io - GitHub library, like:

  • natsOptions, general connetion options for the NATS Server, see Changed configuration properties - NATS.js while others are only used inside the plugin, to configure its behavior, like:
  • drainOnClose, flag (by default false) to enable the drain of last data from the NATS connection when the plugin has to close
  • enableDefaultNATSServer, flag (by default false) to enable connections to public NATS Demo Server (so if a NATS server is not specified and this flag is disabled an Error will be raised), useful for a fast start on tests and examples; default setting is to avoid connections to that (external and public) server, for example by plugin configuration mistake

all plugin options are optional and have a default value set in the plugin.

Default NATS Server in the plugin is set to the public demo NATS Server nats://demo.nats.io:4222, to be able to do a quick start; anyway note that in some cases it could not be reachable (for example by corporate firewall rules), so even plugin tests could fail in that case. To perform some local tests, it's possible to use a NATS Server Docker image from standard image NATS - DockerHub; for convenience, they are defined in package.json like docker:nats:start and the same for log|process|stop etc.

License

Licensed under Apache-2.0.