🚀 DAY 3 OF LAUNCH WEEK: Introducing Webhook Events for Pull Request Scans.Learn more →
Socket
Book a DemoInstallSign in
Socket

typed-subjects

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-subjects

Making type-safe calls via NATS

latest
Source
npmnpm
Version
1.0.19
Version published
Maintainers
1
Created
Source

This library provides high level abstractions over the NATS messaging system, allowing you to make type-safe synchronous and asynchronous calls.

Typical use case is to use it in a microservices architecture for inter-service communication.

Usage

import {connectSubjects, drainWorkerQueues} from "typed-subjects"
import {connect, NatsConnection} from "nats"

// Obtain connection to NATS
const natsConnection = await connect({})

// Define shape of used subjects (API between your components)
const subjects = {
    getPaymentStatus: new RemoteProcedure<{id: number}, {status: PaymentStatus}>("payments.getStatus")
    payments: new TypedSubject<PaymentUpdate>("payments.update"),
}

enum PaymentStatus = {OPEN, PAID, CANCELLED}
type PaymentUpdate = {id: number; status: PaymentStatus}

// Connect subjects to NATS
connectSubjects(subjects, natsConnection)

// Implement/subscribe subjects

subjects.payments.getPaymentStatus.implement(async ({id}) => {
    return {status: PaymentStatus.PAID}
}, {
  // concurrency: number; // Number of concurrent messages processed 
  // timeout?: number; // Max time to process the message
  // middleware: Middleware | Middleware[]; // Middleware to wrap the handler
  // queue?: string; // NATS queue name (used for horizontal scaling)
})

subjects.payments.payments.subscribe(async ({id, status}) => {
    console.log("Got payment update", {id, status})
})

// Call/publish
const {status} = await subjects.getPaymentStatus.request({id: 1})
subjects.payments.payments.publish({id: 1, status: PaymentStatus.PAID})

// At the end, call drainWorkerQueues to wait for all messages to be processed
await drainWorkerQueues()

Main components

TypedSubject. Most basic component. Implements publish/subscribe pattern. Used to to publish messages of certain type.

type PaymentUpdate = {id: number; status: PaymentStatus}
const paymentUpdate  = new TypedSubject<PaymentUpdate>("payments.update")

FilteringSubject. Subject that will allow filtering of data based on partial properties of transferred message. Filter is defined by subject template. Implemented using NATS wildcards. Typical usage is to create subject for particular data, ie listen to object updates by its ID.

type PaymentUpdate = {id: number; status: PaymentStatus}
const paymentUpdate  = new FilteringSubject<PaymentUpdate>("payments.update.$id")

RemoteProcedure. Synchronous remote procedure with single optional request object and single optional result object. Implements NATS request/reply pattern.

const getPaymentStatus  = new RemoteProcedure<{id: number}, {status: PaymentStatus}>("payments.getStatus")

Helper functions

drainWorkerQueues. Finish processing of all messages in the worker queues. This function is useful when you want to wait for all messages to be processed before shutting down the application.

Keywords

nats

FAQs

Package last updated on 15 Jul 2025

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