Building Mesh With SAP Event Mesh – MQTT Client

Introduction –

What’s the secret behind the powerful mesh 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 for respective details.

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.

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}}.hana.ondemand.com', 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.

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

References –

Service details – https://help.sap.com/docs/SAP_ADVANCED_EVENT_MESH

Discovery Center – https://discovery-center.cloud.sap/serviceCatalog/event-mesh?region=all&tab=feature