cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CAP - Event Mesh Consumption of Messages Not working

mani_ac
Participant
0 Kudos
176

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" 
        } 
      }
    }
  }

 

Accepted Solutions (0)

Answers (1)

Answers (1)

Willem_Pardaens
Product and Topic Expert
Product and Topic Expert

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'
})

  

mani_ac
Participant
0 Kudos

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 };

 

Willem_Pardaens
Product and Topic Expert
Product and Topic Expert
0 Kudos
Same issue here, don't define 'messaging' inside the function call only.