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

tasu

v5.0.0

Published

A NATS-based request/pub-sub message transport for Node.js services

Downloads

57

Readme

icon

Tasu

Tests status Coverage Status Known Vulnerabilities

An async/await wrapper over node-nats, designed to easily integrate with your microservice code. Taşuu (ташуу) is 'transport' in Kyrgyz.

Installation

npm i tasu

Usage

Create an instance of the transport:

const Tasuu = require('tasu');

async function main()  {
    ...
    const tasu = new Tasuu({group: 'some-service', ...});
    await tasu.connected();
}

Publish a request and get a response via tasu.request() on one end:

const {bar} = await tasu.request('foo', {arg: 1});

Note: this method uses requestOne inside, no need to worry about max
responses

Subscribe and respond to a request on the other:

tasu.listen('foo', async ({arg}, respond) => {
    const bar = await someDatabaseCall(arg);
    return {bar};
});

Note: A listener is automatically added to queue group foo.listeners; errors of the handling function are caught and sent back as error response

Publish an event on one end:

tasu.publish('some.package.sent', {...});

Subscribe and process as worker queue on the other:

 tasu.process('*.package.sent', (pack, subject) => {
    console.log(subject, pack);
});

listen, subscribe and process methods return an integer subscription ID (SID) which can be used to unsubscribe from a subject:

const sid = tasu.process('*.package.sent', (pack, subject) => {
    console.log(subject, pack);
});

...

tasu.unsubscribe(sid);

The above technique is useful for websocket connections.

Close NATS connection (if needed):

tasu.close();

Configuration

You can set the following config options while creating a new Tasu instance:

  • url - NATS connection urls, default: nats://localhost:4222
  • group - group name this transport will be assigned to, default: default
  • requestTimeout - how long to wait for response on NATS requests, default: 10000
  • level - set log level for provided logger. Default is debug
  • logger - define custom logger. Should basically have info, debug, error methods.

API

  • connected() - returns a promise resolved on successful connection to NATS server. You basically always want this resolved before proceeding with Tasu.

  • publish(subject, message) - optimistically publish a message to a subject. TODO: make this return a promise too.

  • listen(subject, async (message)=>{...}) - subscribes to a subject and respond via handler function. Handler function must be defined via async, it gets message object as the only argument and must return something as successful response. Errors are caught and sent back as error response. Returns subscription id.

  • subscribe(subject, (message, replyTo, subject)=>{...}) - subscribes to a subject and process messages with handler function. All memebers of the group will get subject messages. Returns subscription id.

  • subOnce(subject, (message, subject)=>{...}) - same as subscribe() but unsubscribes immediately after receiving a message.

  • process(subject, (message, subject)=>{...}) - subscribes to a subject as a queue worker and processes messages via handler function. Messages will be distributed randomly among free group members. Returns subscription id.

  • request(subject[, message]) - performs a request and returns a response promise.

  • unsubscribe(sid) - unsubscribes from subscription identified by sid (returned by subscribe, subOnce, process, listen)

  • close() - closes connection to NATS. Always try to exit gracefully, otherwise the connection will persist until next heartbeat.

Credits

Icons by icons8