An open API service indexing awesome lists of open source software.

https://github.com/jasonmejane/ts-post

Flexible PubSub messaging bus system for node and browser applications
https://github.com/jasonmejane/ts-post

bus event messaging publish-subscribe pubsub typescript

Last synced: 9 months ago
JSON representation

Flexible PubSub messaging bus system for node and browser applications

Awesome Lists containing this project

README

          

# ts-post

![](ts-post.png "ts-post logo")


Flexible PubSub messaging bus system for node and browser applications.





Release
 

ts-post on npm
 

Package size
 

Licence


Dependencies
 

Issues
 



Node.js CI
 

Coverage
 

## Install

In terminal, run:
```sh
npm i ts-post
```

## Basic usage

ts-post allows you to create multiple buses to better segment and handle what messages the subscribers will receive or not.
This encourages you to have a well defined type for message data going through each bus, helping limiting potential errors where callbacks would have tried to handle different objects than expected.
The singleton instance of Port has to be created globally, in order to be accessible everywhere in your app without risk of undelivered messages.
By default, a bus named `default` is created.

### Import

```typescript
import { Message, Post, Subscription } from 'ts-post';
```

### Example

```typescript
const post = Post.getInstance;

// Create a new bus called foo, which will dispatch messages only to foo subcribers
post.createBus('foo');

// Subscribe to the bus
const sub = post.subscribe('foo', {
callback: async (message: Message) => {
console.log(`Message timestamp: ${message.getTimestamp()} - issuer: ${message.getIssuer()}`);
await doSomethingWithData(message.getData());
},
errorHandler: (error) => { console.error(error); }
});

// Publish a message into the bus
await post.publish('foo', new Message({ id: 'bar', description: 'some data', available: 104 }, 'FooService'));

// Unsubscribe
sub.unsubscribe();
```

## Options and data

### Subscriber
The available options are:
- `callback` (required): the callback to execute when receiving a message
- `errorHandler` (optional): the callback to execute in case of exception while executing the callback
- `delay` (optional): the delay before executing the callback
- If undefined, the callback will be executed immediatly and synchronously (according to its order in the subscribers list)
- If >= 0, the callback will be put in the event loop using `setTimeout`

When subscribing, the returned subscriber can call `.unsubscribe()` to remove the subscription to the bus.

### Message
When creating a message to be published, the options are:
- `data` (required): the actual data to send
- `issuer` (optional): the app component/service/... responsible for the message publishing

The data sent is packaged with additional info:
- `getData` returns the data sent in the message
- `getTimestamp` returns the creation time (in ms) of the message
- `getIssuer` returns the issuer of the message (if defined)

## Contribute

Please feel free to suggest features or bug fix through Git issues. Pull Requests for that are also more than welcome.