GithubHelp home page GithubHelp logo

ts-event-bus's Introduction

TS-simple-event-bus

npm

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

Version 2.XX

Version 2 adds a typing that is defined once at the bus initialisation.

Use cases

Simple publish and subscribe:

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

// The typing is a nested object with queue names as keys and message payload as value
type EventBusTyping = {
  queue: {
    a: string;
    b: number;
  }
};

// Use a function to generate a unique id. You should provide the typing as well
const bus = new EventBus<EventBusTyping>(uuid);

bus.subscribe({ queue: "queue", callback: (message) => {
  console.log(message.id, message.a, message.b);
} });

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

Waiting for a response:

type EventBusTyping = {
  "event-response": {
    event: string,
  },
  "event-request": {
    event: string,
  },
};
const bus = new EventBus<EventBusTyping>(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" }

Version 1.XX

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: "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" }

ts-event-bus's People

Contributors

sangrene avatar

Watchers

 avatar  avatar

ts-event-bus's Issues

Fix typo in readme

Current:
bus.publish("queue", { a: "a", b: 1 });

Expected:
bus.publish({ queue: "queue", { a: "a", b: 1 } });

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.