-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageFunctions.js
More file actions
30 lines (26 loc) · 971 Bytes
/
Copy pathmessageFunctions.js
File metadata and controls
30 lines (26 loc) · 971 Bytes
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
// messageFunctions.js
const { Message, ServiceBus } = require('./models');
const emitMessages = async (io, topicName, socketName) => {
try {
const pendingMessages = await Message.findAll({ where: { status: 'Pending', topic: topicName } });
pendingMessages.forEach((message) => {
io.emit(socketName, { "message": message.content });
message.status = 'Processed';
message.save();
});
} catch (error) {
console.error('Error fetching and emitting messages:', error);
}
};
const sendMessageToBus = async (io, topicName, content) => {
try {
if (ServiceBus.hasTopic(topicName)) {
await ServiceBus.publish(topicName, content);
} else {
throw new Error('Topic not found in the ServiceBus');
}
} catch (error) {
console.error('Error sending message to the bus:', error);
}
};
module.exports = { emitMessages, sendMessageToBus };