2 weeks ago
I am trying to test the event mesh with a simple emit and consumption service in SAP CAP. I am able to publish a message to the topic, and it is visible in the event mesh dashboard under the queue. However, when I try to consume the message, it is not working. The count in the queue is not decreasing, and the console.log inside messaging.on is not being triggered. I am not sure what mistake I am making.
this.on('messagePersistence', async (req) => {
try {
const messaging = await cds.connect.to('messaging')
messaging.emit({
event: 'test/cap/dev/publisher',
data: `Hello`
})
messaging.on('test/cap/dev/publisher', msg => {
console.log(msg.data)
})
console.log('Messaging Emitted Done')
return 'OK'
}
catch (e) {
console.log(e)
}
});
"cds": {
"requires": {
"auth": "xsuaa",
"messaging": {
"kind": "local-messaging",
"[production]": {
"kind": "enterprise-messaging"
}
}
}
}
Your listener is only defined in the "this.on('messagePersistence')" function block, so gets deleted the moment the function executed. Try this:
const messaging = await cds.connect.to('messaging')
messaging.on('test/cap/dev/publisher', msg => {
console.log(msg.data)
})
this.on('messagePersistence', async (req) => {
await messaging.emit({
event: 'test/cap/dev/publisher',
data: `Hello`
})
console.log('Messaging Emitted Done')
return 'OK'
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have changed it to the below but still the message is only successfully emitting but I dont see it is printing msg.data anytime.
const cds = require('@sap/cds');
class CatalogService extends cds.ApplicationService {
async init() {
const messaging = await cds.connect.to('messaging')
messaging.on('test/cap/dev/publisher', msg => {
console.log(msg.data)
})
this.on('messagePersistence', async (req) => {
await messaging.emit({
event: 'test/cap/dev/publisher',
data: `Hello`
})
console.log('Messaging Emitted Done')
return 'OK'
})
return super.init();
}
}
module.exports = { CatalogService };
User | Count |
---|---|
58 | |
10 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.