Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yss14/node-redis-eventbus
A simple event bus powered by node-redis to communicate between multiple node instances
https://github.com/yss14/node-redis-eventbus
eventbus eventbus-library events node-redis nodejs pubsub redis
Last synced: 4 days ago
JSON representation
A simple event bus powered by node-redis to communicate between multiple node instances
- Host: GitHub
- URL: https://github.com/yss14/node-redis-eventbus
- Owner: yss14
- License: mit
- Created: 2018-01-09T14:27:31.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-21T08:08:21.000Z (almost 7 years ago)
- Last Synced: 2024-04-26T22:22:40.705Z (9 months ago)
- Topics: eventbus, eventbus-library, events, node-redis, nodejs, pubsub, redis
- Language: TypeScript
- Size: 57.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM](https://nodei.co/npm/node-redis-eventbus.png)](https://npmjs.org/package/node-redis-eventbus)
[](https://yss14.visualstudio.com/node-redis-eventbus/_build/index?definitionId=1)# node-redis-eventbus
A simple event bus powered by redis to communicate between multiple node instances.## Install
```bash
npm install --save node-redis-eventbus//or
yarn add node-redis-eventbus
```## Usage
## Async/await pattern
```typescript
//Wrap example by an async function
(async () => {
//Create new event bus instance with a unique identifier
const eventBus = await EventBus.create('myEventBus');//Add listener and wait until it's binded successfully
await eventBus.on('msg', (payload) => {
console.log(`Received message: ${payload}`);eventBus.destory();
process.exit();
});console.log('Listening to msg event');
setTimeout(() => {
//Emit event to all listeners on the 'msg' event
eventBus.emit('msg', 'Hello');
}, 5000);
})();//Somewhere else in your code get reference to the event bus
const eventBus = EventBus.getByName('myEventBus');
eventBus.emit('msg', 'Hello?');
```## Promise style
```typescript
//Create new event bus instance with a unique identifier
EventBus.create('myEventBus').then((eventBus) => {
//Add listener and wait until it's binded successfully
eventBus.on('msg', (payload) => {
console.log(`Received message: ${payload}`);eventBus.destory();
process.exit();
}).then(() => {
console.log('Listening to msg event');setTimeout(() => {
eventBus.emit('msg', 'Hello');
}, 5000);
})
})//Somewhere else in your code get reference to the event bus
const eventBus = EventBus.getByName('myEventBus');
eventBus.emit('msg', 'Hello?');
```## Docs
```typescript
//Register listener for specific event. To avoid sideeffects, you have to wait for the promise to resolve
on(event: string, callback: (payload: T) => void): Promise;//Emit event on the event bus with passed payload
emit(event: string, payload: T): void;//Sends ping to all listeners and waits maximum milliseconds
//Returns true if at least clients responded, false otherwise
ping(timeout?: number, minResponseCount?: number): Promise;//Destroy this event bus instance and disconnect from redis
//Watch out: If there are other clients connected, these instances will not be destroyed!
destroy(): void;//Property which indicated wether the event bus is connected
connected: boolean;//Creates a new event bus with a unique name and optional node-redis client options
static create(name: string, clientOpts?: Redis.ClientOpts): Promise;//Access existing event bus instance by unique name
static getByName(name: string): EventBus;
```## Typescript
This package automatically ships a `d.ts` definition file!