Skip to Content
Technical Articles
Author's profile photo Gaurav Abbi

Cloud Events SDK, Kyma runtime and SAP Event Mesh

As I mentioned in a previous post, it is possible to now produce or publish events inside the Kyma runtime. This opens up a lot of possibilities for developers building microservice-based architecture and need to have asynchronous workflows.

Since the events produced and consumed inside Kyma runtime conform to CloudEvents specification, it makes it easy to leverage CloudEvents SDKs for various technologies to work with events.

Producing the event

Let’s look at an example of using cloudevents sdk  in a serverless function. It will produce events that will be stored in SAP Event Mesh.

Note: It is now possible to use SAP Event Mesh instead of NATS as the eventing backend in managed Kyma runtime and this sample is using SAP Event Mesh. Once configured, all events coming from connected systems such as SAP Commerce Cloud, SAP Cloud for Customer, and others as well as events produced inside Kyma runtime will be stored in the event mesh.

First, we need to declare the dependencies:

{ 
  "name": "event-producer",
  "version": "1.0.0",
  "dependencies": {
    "cloudevents" : "4.0.3",
    "axios": "0.21.1"
  }
}

 

The code to publish the event itself is pretty straightforward.

const axios = require("axios").default;
const { HTTP, CloudEvent } = require("cloudevents");

module.exports = { 
  main: async function (event, context) {
    const ce = new CloudEvent({ 
      type: "sap.kyma.custom.internal.product.viewed.v1", 
      source: "/default/my.kyma/test", 
      data: {'produtId' : '123'},
      datacontenttype: "application/json" },
      );
    
    const message = HTTP.structured(ce);
   
    var publishResponse = await axios({
      method: 'post',
      url: process.env.PUBLISHER_URL,
      data: message.body,
      headers: message.headers 
    })
  }
}

As you can see, I only need to specify only the minimal attributes in the CloudEvent. The HTTP request is then created from the message object.

Here I am using the structured mode, hence

HTTP.structured(ce).

 

You could very well use the binary mode by calling

HTTP.binary(ce)

 

Then I can simply apply the HTTP payload and headers and send them using axios.

Consuming the event

There is nothing special when it comes to consuming the event.

I simply need to create a subscription

apiVersion: eventing.kyma-project.io/v1alpha1
kind: Subscription
metadata:
  name: event-consumer
spec:
  filter:
    filters:
    - eventSource:
        property: source
        type: exact
        value: ""
      eventType:
        property: type
        type: exact
        value: sap.kyma.custom.internal.product.viewed.v1
  protocol: ""
  protocolsettings: {}
  sink: http://event-consumer.dev.svc.cluster.local #this is the service name and port

If you notice, I am not specifying the source. Once Kyma runtime is configured to use SAP Event mesh, it will take that as the default source.

All the required configurations to receive the event from the SAP Event Mesh will be automatically created.

 

Takeaways

  • Use CloudEvents SDK to work with events in Kyma runtime.
  • You can now switch to SAP Event Mesh as the backend instead of NATS for Kyma eventing.

Assigned tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.