Created
July 3, 2020 11:49
-
-
Save VineetKumarKushwaha/6338f8ffe6a5e64639f6e18477c57ba6 to your computer and use it in GitHub Desktop.
Publisher-Subscriber Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pubSub = (() => { | |
const topicSubscriberMap = new Map(); | |
const throwError = (topic) => { | |
throw new Error(` | |
No ${topic} named topic found in the registry. | |
Available topics:- ${JSON.stringify(getAllTopic())} | |
`); | |
}; | |
const getAllTopic = () => Array.from(topicSubscriberMap.keys()); | |
const createTopic = (topic) => { | |
if (topicSubscriberMap.get(topic)) throw new Error("Topic is already registered."); | |
topicSubscriberMap.set(topic, []); | |
return 1; | |
}; | |
const publish = (topic, data) => { | |
if (topicSubscriberMap.has(topic)) { | |
const subscribers = topicSubscriberMap.get(topic) || []; | |
subscribers.forEach(subscriberCb => subscriberCb && subscriberCb(data)); | |
return 1; | |
} | |
throwError(topic); | |
}; | |
const subscribe = (topic, cb) => { | |
if (topicSubscriberMap.has(topic)) { | |
const subscribers = topicSubscriberMap.get(topic) || []; | |
subscribers.push(cb); | |
topicSubscriberMap.set(topic, subscribers); | |
return 1; | |
} | |
throwError(topic); | |
} | |
const unSubscribe = (topic, cb) => { | |
if (topicSubscriberMap.has(topic)) { | |
let subscribers = topicSubscriberMap.get(topic) || []; | |
subscribers = subscribers.filter(subscriberCb => subscriberCb !== cb); | |
topicSubscriberMap.set(topic, subscribers); | |
return 1; | |
} | |
throwError(topic); | |
}; | |
return { | |
publisher: { | |
publish, | |
createTopic | |
}, | |
subscriber: { | |
unSubscribe, | |
subscribe | |
}, | |
getAllTopic | |
}; | |
})() | |
/** | |
const callback1 = (data) => console.log("callback1 click", data); | |
const callback2 = (data) => console.log("callback2 click", data); | |
const callback3 = (data) => console.log("callback3 keydown", data); | |
const callback4 = (data) => console.log("callback4 both", data); | |
pubSub.publisher.createTopic("click"); | |
pubSub.subscriber.subscribe("click", callback1); | |
pubSub.subscriber.subscribe("click", callback2); | |
pubSub.publisher.publish("click", "click happen") | |
pubSub.subscriber.unSubscribe("click", callback2) | |
pubSub.publisher.publish("click", "another click happen") | |
pubSub.publisher.createTopic("keydown"); | |
pubSub.subscriber.subscribe("click", callback3); | |
pubSub.subscriber.subscribe("click", callback4); | |
pubSub.subscriber.subscribe("click", callback4); | |
pubSub.subscriber.subscribe("keydown", callback3); | |
pubSub.subscriber.subscribe("keydown", callback4); | |
pubSub.publisher.publish("click", "click") | |
pubSub.publisher.publish("keydown", "keydown") | |
**/ |
Difference between observer vs pub-sub pattern
In the Observer Pattern, the Subject and observer know each other. Subject maintains a list of all the observer and call when needed
In the Pub-Sub Pattern, publishers and subscribers do not know each other.
The subscriber subscribes topic and publishers publish data on the topic.
So topic handlers know each topic and their respective subscribers.
Implementation of observer link here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
“In software architecture, publish-subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead categorize published messages into classes without knowledge of which subscribers if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.
From Wikipedia