AsyncAPI Conf on Tour 2023

Madrid Edition

October, 2023 | Madrid, Spain

5 days until the end for Call for Speakers.

Message validation in runtime

Found an error? Have a suggestion?Edit this page on GitHub

Introduction

In this tutorial, you'll learn how to validate messages (events) that are sent to your AsyncAPI application.

Background context

Message validation can be performed at both the producer and consumer levels. Message validation requires the participation of the producer, consumer, and broker. We will learn how to validate messages at the consumer level by discarding invalid messages based on the parameters provided.

You will be using the Eclipse Mosquitto broker. The MQTT protocol provides a lightweight method of messaging using a publish/subscribe model. You will also use an MQTT client that runs an MQTT library and connects to an MQTT broker over a network. Here publishers and subscribers are MQTT clients. The publisher and subscriber labels refer to whether the client is publishing or subscribing to receive messages.

In the previous tutorial, you generated your application using the AsyncAPI Generator Node.js template.

Remember

If you did not follow the previous tutorial and do not have an asyncapi.yaml file ready, then generate one by running: asyncapi new --example=tutorial.yml --no-tty.

Next, generate a server by running:

1
2
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/nodejs-template -o output -p server=mosquitto
cd output && npm install

Now you will be validating the messages which you will be sending to your application using a Mosquitto broker and an MQTT client.

Validate messages

In this step, you will send a message to your application using an MQTT broker and check the errors logged when you accidentally send an invalid message.

  1. Start your generated application.
1
npm start
  1. Let's send a message:
1
mqtt pub -t 'light/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": "3", "sentAt": "2017-06-07T12:34:32.000Z"}'

Go back to the previous terminal and check if your application logged the streetlight condition you just sent, with errors related to the invalid message. You should see something displayed in the terminal similar to the following:

1
2
3
light/measured was received:
{ id: 1, lumens: '3', sentAt: '2017-06-07T12:34:32.000Z' }
❗  Message Rejected. data.lumens should be integer

Here, you can see that the property lumens has type integer, but you are sending a message with type string.

1
2
3
4
5
6
7
8
9
10
11
12
13
  message:
        name: lumensInfo
        payload:
          type: object
          properties:
            id:
              type: integer
              minimum: 0
              description: Id of the streetlight.
            lumens:
              type: integer
              minimum: 0
              description: Light intensity measured in lumens.
  1. Send a correct message to your application:
1
mqtt pub -t 'light/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, "sentAt": "2017-06-07T12:34:32.000Z"}'

You can see that your generated application received a message in the terminal:

1
2
light/measured was received:
{ id: 1, lumens: 3, sentAt: '2017-06-07T12:34:32.000Z' }

This indicates that your message is valid and the application recieved it correctly.

Summary

In this tutorial, you learned how to connect your generated application to an MQTT broker, send messages through it, identify when an invalid message is sent to your application, and how to correct an invalid message.

Next steps

Now that you've completed this tutorial, enjoy our AsyncAPI message validation guide.


Was this helpful?
Help us improve the docs by adding your contribution.
OR
Github:AsyncAPICreate Issue on GitHub