
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
typed-subjects
Advanced tools
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.
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()
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")
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.
FAQs
Making type-safe calls via NATS
We found that typed-subjects demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.