Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nats

Package Overview
Dependencies
Maintainers
3
Versions
195
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nats

Node.js client for NATS, a lightweight, high-performance cloud native messaging system

  • 2.9.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
267K
increased by0.73%
Maintainers
3
Weekly downloads
 
Created

What is nats?

The 'nats' npm package is a client library for the NATS messaging system, which is a high-performance, lightweight, and open-source messaging system for cloud-native applications, IoT messaging, and microservices architectures. It provides publish-subscribe, request-reply, and distributed queueing functionalities.

What are nats's main functionalities?

Publish-Subscribe

This feature allows you to publish messages to a subject and have multiple subscribers receive those messages. The code sample demonstrates how to set up a simple publish-subscribe system where a message is published to the 'updates' subject and received by a subscriber.

const { connect, StringCodec } = require('nats');

(async () => {
  const nc = await connect({ servers: 'demo.nats.io:4222' });
  const sc = StringCodec();

  // Subscriber
  const sub = nc.subscribe('updates');
  (async () => {
    for await (const m of sub) {
      console.log(`Received a message: ${sc.decode(m.data)}`);
    }
  })();

  // Publisher
  nc.publish('updates', sc.encode('Hello, NATS!'));
})();

Request-Reply

This feature allows you to send a request and receive a reply, enabling synchronous communication between services. The code sample shows how to set up a responder that listens for requests on the 'help' subject and a requester that sends a request and waits for a reply.

const { connect, StringCodec } = require('nats');

(async () => {
  const nc = await connect({ servers: 'demo.nats.io:4222' });
  const sc = StringCodec();

  // Responder
  nc.subscribe('help', {
    callback: (err, msg) => {
      if (err) {
        console.error(err);
      } else {
        msg.respond(sc.encode('I can help!'));
      }
    }
  });

  // Requester
  const msg = await nc.request('help', sc.encode('Need assistance'), { timeout: 1000 });
  console.log(`Received reply: ${sc.decode(msg.data)}`);
})();

Distributed Queueing

This feature allows you to distribute tasks among multiple workers, ensuring that each task is processed by only one worker. The code sample demonstrates how to set up two workers that listen on the 'tasks' subject and a publisher that sends tasks to be processed.

const { connect, StringCodec } = require('nats');

(async () => {
  const nc = await connect({ servers: 'demo.nats.io:4222' });
  const sc = StringCodec();

  // Worker 1
  nc.subscribe('tasks', { queue: 'workers' }, (err, msg) => {
    if (err) {
      console.error(err);
    } else {
      console.log(`Worker 1 received: ${sc.decode(msg.data)}`);
    }
  });

  // Worker 2
  nc.subscribe('tasks', { queue: 'workers' }, (err, msg) => {
    if (err) {
      console.error(err);
    } else {
      console.log(`Worker 2 received: ${sc.decode(msg.data)}`);
    }
  });

  // Publisher
  nc.publish('tasks', sc.encode('Task 1'));
  nc.publish('tasks', sc.encode('Task 2'));
})();

Other packages similar to nats

Keywords

FAQs

Package last updated on 02 Nov 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc