Giter Club home page Giter Club logo

nats-toolkit-ts's Introduction

Nats Typescript Toolkit

This is a work in progress actively under development

A toolkit to build distributed services in Typescript on top of Nats. It exposes a straightforward API abstracting most of the complexity of using Nats with sensible defaults and fully typed. It takes care of common scenarios such as payload validation, retry and more.

Example

// Declare your payload types using zod schemas
const subscriptions = {
  'foo.bar': z.object({
    message: z.string(),
  }),
};

// Build a broker, passing in your types
const broker = await getBroker({ subscriptions });

// Use the broker with complete type inference
broker.subscribe('foo.bar', ({ data }) => {
  console.log(data.message); // Type = { message: string }
});

broker.publish('foo.bar', (wrap) => wrap({ message: 'hello' }));
broker.publish('foo.bar', (wrap) => wrap(10)); // Argument of type 'number' is not assignable to parameter of type '{ message: string; }'

API

The toolkit provides three different means of communicating.

Subscribers

Subscriptions are based on the base Nats subscriptions, offering a simple fire and forget mechanism. Messages are not guaranteed to be delivered and no acknowledgement is sent back to the publisher.

Request-Replies

Similar to subscriptions but the sender receives a response from the listener, similarly to a classical client-server architecture with a request and a response.

Consumers

Consumers are based on Nats JetStreams to offer messages retention. Using this mechanism guarantees that messages are delivered.

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.