process-pubsub v1.0.2
Process PubSub
A tiny library that enables node applications to publish events to one multiple interested consumers asynchronously, without coupling the producers to the consumers.
Works only on single process, to provide effective way to decouple producers from consumers
How Process PubSub works:
Installation
npm install process-pubsub --saveFeatures
- Simplicity & flexibility
- Light weight
Usage
Channels & Topics
- Channel: is a logical container used to send and receive messages, publishing/receiving messages should be through a channel
- Topic: is a logical container within channel used to send and receive messages, publishing/receiving messages should be through a topic registered within a channel
Subscription
subscribe function can be used to subscribe to one or more channels and topics, through Subscribtion object:
Subscription:
channelString Channel nametopicString Topic nameonceBoolean, optional, default =fasleGet notified about specific event only oncecallbackFunction(message, metadata) Callback function will be invoked when message published to achanneland/ortopic
Simple subscription:
import {PubSub} from 'process-pubsub';
const pubsub = new PubSub();
pubsub.subscribe({
channel: 'orders',
topic: 'created',
callback: (message, metadata) => {
console.log(message);
}
});Subscription to get notified for all messages in specific channel is also possible using * with topic field:
import {PubSub} from 'process-pubsub';
const pubsub = new PubSub();
pubsub.subscribe({
channel: 'orders',
topic: '*',
callback: (message, metadata) => {
console.log(message);
}
});Subscribe to all messages published to any
topicunderchannel"orders", socallbackhandler will be call if we publish messages to "create", "updated" topic for example or any other topic under "orders" channel
Once subscription
Setting once field to true will notify the subscriber with the event for only one-time, after that the subscription will be removed.
import {PubSub} from 'process-pubsub';
const pubsub = new PubSub();
pubsub.subscribe({
channel: 'orders',
topic: 'created',
once: true,
callback: (message, metadata) => {
console.log(message);
}
});Message metadata
callback function will also receive metadata object with message, which includes the following fields:
channelThe current message destination channeltopicThe current message destination topic (If subscriber is listening to all topic using*under specific channel, metadata will be very useful)
Publishing
We can publish directly to specific channel and topic:
import {PubSub} from 'process-pubsub';
const pubsub = new PubSub();
pubsub.publish('orders', 'created', {id: 1, amount: 120});
- This message will be received by all subscribers of channel
ordersand topiccreated- This message will be received also by subscribers of channel
ordersand topic*
We can publish to all topics registered within a specific channel, by using * for topic
import {PubSub} from 'process-pubsub';
const pubsub = new PubSub();
pubsub.publish('orders', '*', {id: 1, amount: 120});This message will be received by all subscribers of channel
orders, whatever the topic the subscriber is registered with
Synchronous Request-Reply
Synchronous Request-Reply style is also possible using publishAndGetReply function, we can publish message and wait for response from first register subscriber, or break the operation if timeout exceeded.
import {PubSub} from 'process-pubsub';
const pubsub = new PubSub();
const response = await pubsub.publishAndGetReply('orders', 'created', {id: 1, amount: 120});License
MIT