Skip to Content
Technical Articles
Author's profile photo Jay Adnure

Building Mesh With SAP Event Mesh – MQTT Client

Introduction –

What’s the secret behind the powerful web which Spider-Man builds?  I don’t have the answer for this, but I can tell you what an Intelligent enterprise makes use of, to build its messaging mesh which is secured, reliable and consistent.

Creating an Enterprise messaging mesh was never easy before then it’s now with help of SAP Event mesh. SAP Event Mesh is a fully managed cloud service that allows applications to communicate through asynchronous events. Experience greater agility and scalability when you create responsive applications that work independently and participate in event-driven business processes across your business ecosystem.

If you are looking for information on how to build an MQTT client-to-consume event mesh, then you are at the right place and if you are looking for how to get started with Event mesh you can refer to the discovery center.

Motivation –

We come across many use cases where we have constrained, low bandwidth, high latency, or unreliable devices where it’s recommended to use the MQTT (Message Queuing Telemetry Transport) protocol. MQTT protocol provides a very cool feature called Quality Of Service (QoS) which guarantees message delivery regardless of network reliability. QoS is supported by different levels, which will look in detail in our example below. Let’s understand the architecture, which we will refer to develop a client-consuming Event Mesh Service.

SAP Event Mesh also supports a couple of other protocols listed below which can be used as per your business requirements –

  1. REST APIs for Messaging – The service provides REST APIs for messaging. You can use these messaging REST APIs to send and receive messages.
  2. Advanced Message Queuing Protocol (AMQP) 1.0 over WebSocket – This is an open standard protocol used for messaging between applications

Architecture –

The above solution diagram, explains how we can build a Business process extension application on BTP Cloud foundry using SAP Event Mesh Service. We will be focusing on building an event mesh client consumed in a client side application. This application will subscribe to the events generated from SAP / Non SAP system and trigger the necessary business flows as configured.

Tutorial –

Before we jump straight into building the client, let’s look at the prerequisites which I had gone through.

  1. SAP Event Mesh Entitlement in SAP BTP.
  2. Development Environment –
    1. Node.js installed on your local machine
    2. Visual Studio Code is installed, which will be our development IDE.
  3. Setup Event Mesh using the developer tutorial  –
    1. Instance creation 
    2. Create Queue

Now we are ready to build our SAP Event Mesh client using Node.js.

  1. Create a node project on your local machine with help of “npm init” command on your local machine using Visual Studio Code IDE.
  2. Install package xb-msg-mqtt-v311
  3. This package/ library comes with a handful of examples that we can use as an inspiration to get started.
    1. Change the working directory to xb-msg-mqtt-v311  folder.
  4. Create a folder config under the directory xb-msg-mqtt-v311 and file name my-options.js.
    1. We have to edit file my-option.js with following details –
    2. 'use strict';
      const fs = require('fs');
      module.exports = {
          oa2: {
              endpoint: 'https://{{COPY FROM SERVICE KEY}}/oauth/token',
              client: '{{COPY FROM SERVICE KEY}}',
              secret: '{{COPY FROM SERVICE KEY}}',
          wss: {
              host: 'enterprise-messaging-messaging-gateway.cfapps.{{YOUR REGION}}',
              port: 443,
              path: '/protocols/mqtt311ws'
          data: {
              source       : 'queue:iotqueue',
              target       : 'topic:apjdl/dlmsgclnt/01/iotqueue',
              payload      : new Buffer.allocUnsafe(20),
              maxCount     : 100000,
              logCount     : 1,
              qos: 1
    3. A couple of values under the data section in my-option.js need to be modified and to do so you have to check the namespace and queue name used in the Event Mesh instance.
  5. Next we have to modify the following 2 files which will point this client to the respective queue where this client sends and receives the data.
    1. examples/publisher.js – Change the topic and filter values as below
      { topic: “apjdl/dlmsgclnt/01/iotqueue”, filter: ‘apjdl/dlmsgclnt/+/#’}
    2. examples/subscriber.js – Change the topic and filter as below
      { topic: “apjdl/dlmsgclnt/01/iotqueue”, filter: ‘apjdl/dlmsgclnt/01/+/#’}
  6. You can also modify the QoS value accroding to your requirement –
    1. At most once (0) – It guarantees its best effort with delivery. A message isn’t acknowledged by the receiver, stored, or redelivered by the sender.
    2. At least once (1) – It guarantees that a message is delivered at least once to the receiver. The message can also be delivered more than once.
  7. It’s time to run the client and see if it can connect to the Event Mesh and start publishing the data. Please run the following command at the root of this folder – xb-msg-mqtt-v31
    1. node examples/publisher.js ../config/my-options.js
    2. Open another terminal to run the subscriber which will receive the data published by the publisher script.
    3. node examples/subscriber.js ../config/my-options.js
  8. Don’t believe it until you see it. A successful connection should result in the following output on your terminal.


The last step would be to deploy the application on BTP, either on the Cloud Foundry environment or the Kyma environment, and you should be able to see the client in action.

What Next?

We have different missions which you can follow to start your Event Mesh journey. To name a few, you can build your own extensions with SAP S/4 HANA, SAP Success Factors, etc.

  1. Extend SAP S/4HANA Cloud on SAP BTP – Discovery center link
  2. Create a custom mobile app to extend HR capabilities – Discovery center link
  3. Extend SAP S/4HANA business processes on SAP BTP – Discovery center link

I hope this blog will help you in your next project where you can consume SAP Event Mesh using the MQTT protocol.

References –

Service details –

Discovery Center –

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jay Adnure
      Jay Adnure
      Blog Post Author

      Please watch out for new offering too - Advanced Event Mesh