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

ts-simple-event-bus

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-simple-event-bus

Simple TS event bus

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
59
decreased by-62.89%
Maintainers
1
Weekly downloads
 
Created
Source

TS-simple-event-bus

A simple event bus that implements a pub / sub pattern. Fully typed.

Use cases

Simple publish and subscribe:

import { EventBus, Callback } from "ts-simple-event-bus";
import { uuid } from "uuid";

type MessagePayload = {
  a: string;
  b: number;
};

// Use a function to generate a unique id.
const bus = new EventBus(uuid);
//
const callback: Callback<MessagePayload> = (message) => {
  console.log(message.id, message.a, message.b);
};

bus.subscribe<MessagePayload>({ queue: "queue", callback });

bus.publish<MessagePayload>("queue", { a: "a", b: 1 });

Waiting for a response:

const bus = new EventBus(idGenerator);
const responseQueueName = "event-response";
bus.subscribe({
  queue: "event-request",
  callback: (msg, id) => {
    bus.publish({ queue: responseQueueName, message: { event: "response" }, id });
  },
});
const result = await bus.publishAndWaitForResponse({
  queue: "event-request",
  responseQueue: responseQueueName,
  message: { event: "request" },
  timeout: 1000,
});
// result will be { event: "response" }

Keywords

FAQs

Package last updated on 01 Apr 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