cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to emit event from CAP to a specific topic

JulianKuipers
Participant
1,973

Hi,

I want to emit and receive events from SAP Event Mesh in my CAP app. We have a dedicated queue for the topic where the events will be emitted to from the CAP app and a dedicated queue for the topic where I'll need to receive the events from.

My service definition:

service Service { 

// Events @topic: 'a/b/c/triggered' 

event triggered : { ID : UUID; parameters : { a : String(2); b: array of String(4); c : array of String(7) } } }

My service logic:

const messaging_receiving = await cds.connect.to("messaging-receiving"); 

const messaging_publishing = await cds.connect.to("messaging-publishing"); 

this.after("CREATE", "Runs", async (each) => { 

const sA = ""; 

const aB = []; 

const aC = []; 

const oPayLoad = { ID: sInterfaceRunID, parameters: { a: sA, b: aB, c: aC, }, }; 

await messaging_publishing.emit( "a/b/c/triggered", oPayLoad ); } });

My cds.requires:

"cds": {
"requires" : {

"messaging-receiving": {
          "kind": "enterprise-messaging-shared",
          "publishPrefix": "$namespace/",
          "subscribePrefix": "$namespace/",
          "format": "cloudevents",
          "queue": {
            "name": "$namespace/v1/flowStatus"
          }
        },
        "messaging-publishing": {
          "kind": "enterprise-messaging-shared",
          "publishPrefix": "$namespace/",
          "subscribePrefix": "$namespace/",
          "format": "cloudevents",
          "queue": {
            "name": "$namespace/v1/startFlow"
          }
        }
}
}<br>

The automatic creation of queues and topics is visible in the SAP Event Mesh. I'm able to receive events, but the ones I emit aren't showing up in the SAP Event Mesh. Any idea what I might be missing?

Kind regards,

Julian

View Entire Topic
david_kunz2
Product and Topic Expert
Product and Topic Expert

Hi juliankuipers ,

CAP never emits events to a queue, hence the `queue` property for publish-only services is ignored.
CAP always emits to a topic. If a queue subscribes to this topic, it will be stored in that queue.

In your specific example, consumers must listen to topic "$namespace/a/b/c/triggered".

Best regards,
David

JulianKuipers
Participant
0 Kudos

Hi david.kunz2,

Thank you for your response.

We indeed want to emit to the topic "$namespace/a/b/c/triggered". In SAP Event Mesh we've got a queue subscribed to this topic.

When we only had one queue for both the emitting topic and the receiving topic, it worked. We saw in the logging of the CAP app that the event was emitted. Now, we do not even see the event get emitted, eventhough the code runs through the emit function.

Any idea where we might have taken a wrong turn with this approach?

Kind regards,

Julian

david_kunz2
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi juliankuipers ,

That's interesting, you should see the `emit` log:

[enterprise-messaging-amqp] - Emit { topic: '<yournamespace>/a/b/c/triggered' }

Your code looks correct and I reproduced it in my setup (it works).Do you see

[cds] - connect to messaging-publishing > enterprise-messaging-amqp { ... }?
JulianKuipers
Participant
0 Kudos

Hi david.kunz2 ,

We've just updated @sap/cds from version 7.3.1. to 7.4.1 and now it's working.

Thank you very much for your help though!

Kind regards,

Julian

david_kunz2
Product and Topic Expert
Product and Topic Expert

Ok, perfect, thanks for the feedback!