-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublishSubscribe.js
63 lines (52 loc) · 2.11 KB
/
publishSubscribe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Import the 'redis' module for handling Publish Subscribe Model functionality.
const redis = require('redis');
// Define channels for communication between publisher and subscriber.
const CHANNELS = {
TEST:'TEST',
BLOCKCHAIN:'BLOCKCHAIN'
}
// Implement the Publish-Subscribe model for blockchain communication.
class PubSub{
constructor({blockchain}){
// Store reference to the blockchain instance.
this.blockchain = blockchain;
// Create Redis clients for publishing and subscribing.
this.publisher = redis.createClient();
this.subscriber = redis.createClient();
// Subscribe to designated channels.
this.subscriber.subscribe(CHANNELS.TEST);
this.subscriber.subscribe(CHANNELS.BLOCKCHAIN);
// Handle incoming messages on subscribed channels.
// on event , if any message event handle the message
this.subscriber.on('message',(channel,message)=>{
this.handleMessage(channel,message);
});
}
// Handle incoming messages based on channel and take appropriate action.
handleMessage(channel,message){
console.log(`Message recieved Channel : ${channel} Message: ${message}`);
const parseMessage = JSON.parse(message);
// console.log(parseMessage);
if(channel === CHANNELS.BLOCKCHAIN){
this.blockchain.replaceChain(parseMessage);
}
};
// Publish a message to a specific channel.
publish({channel,message}){
this.publisher.publish(channel,message);
}
// Broadcast the blockchain to all subscribers.
broadcastChain(){
this.publish({
channel:CHANNELS.BLOCKCHAIN,
// converting the array chain into String
message:JSON.stringify(this.blockchain.chain)
})
}
}
// const checkPubSubObj = new PubSub();
// publishing the message in the test channel with data "hello"
// 1 sec delay taken for process of publishing and subscribing
// setTimeout( () => checkPubSubObj.publisher.publish(CHANNELS.TEST, "Helloo"), 1000);
// Export the PubSub class for use in other modules.
module.exports = PubSub;