Giter Site home page Giter Site logo

smartiniongithub / fastify-nats-client Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 107 KB

Fastify Plugin for Client connections to NATS Server

License: Apache License 2.0

JavaScript 99.32% HTML 0.68%
fastify fastify-plugin nats nats-client nats-messaging queue messaging

fastify-nats-client's Introduction

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.


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.