项目作者: yss14

项目描述 :
A simple event bus powered by node-redis to communicate between multiple node instances
高级语言: TypeScript
项目地址: git://github.com/yss14/node-redis-eventbus.git
创建时间: 2018-01-09T14:27:31Z
项目社区:https://github.com/yss14/node-redis-eventbus

开源协议:MIT License

下载


NPM

node-redis-eventbus

A simple event bus powered by redis to communicate between multiple node instances.

Install

  1. npm install --save node-redis-eventbus
  2. //or
  3. yarn add node-redis-eventbus

Usage

Async/await pattern

  1. //Wrap example by an async function
  2. (async () => {
  3. //Create new event bus instance with a unique identifier
  4. const eventBus = await EventBus.create('myEventBus');
  5. //Add listener and wait until it's binded successfully
  6. await eventBus.on('msg', (payload) => {
  7. console.log(`Received message: ${payload}`);
  8. eventBus.destory();
  9. process.exit();
  10. });
  11. console.log('Listening to msg event');
  12. setTimeout(() => {
  13. //Emit event to all listeners on the 'msg' event
  14. eventBus.emit('msg', 'Hello');
  15. }, 5000);
  16. })();
  17. //Somewhere else in your code get reference to the event bus
  18. const eventBus = EventBus.getByName('myEventBus');
  19. eventBus.emit('msg', 'Hello?');

Promise style

  1. //Create new event bus instance with a unique identifier
  2. EventBus.create('myEventBus').then((eventBus) => {
  3. //Add listener and wait until it's binded successfully
  4. eventBus.on('msg', (payload) => {
  5. console.log(`Received message: ${payload}`);
  6. eventBus.destory();
  7. process.exit();
  8. }).then(() => {
  9. console.log('Listening to msg event');
  10. setTimeout(() => {
  11. eventBus.emit('msg', 'Hello');
  12. }, 5000);
  13. })
  14. })
  15. //Somewhere else in your code get reference to the event bus
  16. const eventBus = EventBus.getByName('myEventBus');
  17. eventBus.emit('msg', 'Hello?');

Docs

  1. //Register listener for specific event. To avoid sideeffects, you have to wait for the promise to resolve
  2. on<T>(event: string, callback: (payload: T) => void): Promise<void>;
  3. //Emit event on the event bus with passed payload
  4. emit<T>(event: string, payload: T): void;
  5. //Sends ping to all listeners and waits maximum <timeout> milliseconds
  6. //Returns true if at least <minResponseCount> clients responded, false otherwise
  7. ping(timeout?: number, minResponseCount?: number): Promise<boolean>;
  8. //Destroy this event bus instance and disconnect from redis
  9. //Watch out: If there are other clients connected, these instances will not be destroyed!
  10. destroy(): void;
  11. //Property which indicated wether the event bus is connected
  12. connected: boolean;
  13. //Creates a new event bus with a unique name and optional node-redis client options
  14. static create(name: string, clientOpts?: Redis.ClientOpts): Promise<EventBus>;
  15. //Access existing event bus instance by unique name
  16. static getByName(name: string): EventBus;

Typescript

This package automatically ships a d.ts definition file!