<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Waleed Ashraf</title>
    <description>The latest articles on DEV Community by Waleed Ashraf (@waleedashraf).</description>
    <link>https://dev.to/waleedashraf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F168826%2Fe85cfbc6-ef83-4b1f-a1d1-77d7957abf58.jpg</url>
      <title>DEV Community: Waleed Ashraf</title>
      <link>https://dev.to/waleedashraf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/waleedashraf"/>
    <language>en</language>
    <item>
      <title>Testing</title>
      <dc:creator>Waleed Ashraf</dc:creator>
      <pubDate>Mon, 17 Feb 2020 22:17:28 +0000</pubDate>
      <link>https://dev.to/waleedashraf/testing-jgj</link>
      <guid>https://dev.to/waleedashraf/testing-jgj</guid>
      <description>&lt;p&gt;Testing&lt;/p&gt;

</description>
    </item>
    <item>
      <title>AsyncAPI for documentation and validation of event-driven architecture</title>
      <dc:creator>Waleed Ashraf</dc:creator>
      <pubDate>Mon, 17 Feb 2020 11:54:04 +0000</pubDate>
      <link>https://dev.to/waleedashraf/asyncapi-for-documentation-and-validation-of-event-driven-architecture-2ap8</link>
      <guid>https://dev.to/waleedashraf/asyncapi-for-documentation-and-validation-of-event-driven-architecture-2ap8</guid>
      <description>&lt;p&gt;In this blog post, you’ll read about what is &lt;a href="https://asyncapi.io/"&gt;AsyncAPI&lt;/a&gt; and how at relayr we use it for validation of documentation of messaging between dozens of Node.js services.&lt;/p&gt;

&lt;h1&gt;
  
  
  OpenAPI and Swagger
&lt;/h1&gt;

&lt;p&gt;Let’s first talk a little about OpenAPI and &lt;a href="https://swagger.io/"&gt;Swagger&lt;/a&gt;. The Swagger specification was founded back in 2010 as a design specification for RESTful APIs. It got a lot of attention from the whole development community and within a few years, it was being used by many big companies. In 2015, the Swagger specification was donated to Linux Foundation and renamed to OpenAPI and is now managed by &lt;a href="https://www.openapis.org/"&gt;OpenAPI Initiative&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  AsyncAPI
&lt;/h1&gt;

&lt;p&gt;The Swagger specification was created in 2010. Nowadays, more and more companies are moving towards event-driven/message-driven and microservices architecture where multiple services communicate with each other over some messaging protocol, such as &lt;a href="https://kafka.apache.org/intro"&gt;Kafka&lt;/a&gt;, &lt;a href="https://www.rabbitmq.com/"&gt;RabbitMQ&lt;/a&gt; or even web sockets.&lt;/p&gt;

&lt;p&gt;This brought the need to have a specification for messaging between services. The AsyncAPI specification solves this problem. It lets you define the schema for messages between services with any kind of protocol. I once called it "Swagger for Kafka" 😄. &lt;a href="https://twitter.com/fmvilas"&gt;@fmvilas&lt;/a&gt; started the project in 2017 at Hitch and now works 24/7 for it 💪. We have around 16 backers on &lt;a href="https://opencollective.com/asyncapi"&gt;OpenCollective&lt;/a&gt; 🎉.&lt;/p&gt;

&lt;p&gt;Let’s now talk about the advantages of AsyncAPI!&lt;/p&gt;

&lt;h2&gt;
  
  
  A contract between multiple parties
&lt;/h2&gt;

&lt;p&gt;AsyncAPI provides a contract for communication between consumers and producers. In our case, there could be multiple consumers and producers so it’s a contract between all of them. It tells what should be included in the payload when a service produces a message and also tells the consumer about the properties in the message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understandable by humans and machines
&lt;/h2&gt;

&lt;p&gt;As AsyncAPI is a schema definition, it is understandable and readable by both machines and humans. The schema can be written in YAML or JSON format which can be easily used with Node.js. It also provides a nice HTML or Markdown document which is human readable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing and validation
&lt;/h2&gt;

&lt;p&gt;AsyncAPI can be used for testing and validation of the payload. At relayr, we wrote the validation library asyncapi-validator which I describe in detail in the Architecture at relayr section of this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Documentation
&lt;/h2&gt;

&lt;p&gt;One other big benefit of using AsyncAPI is that it provides nice documentation for all the messages in-between your services. You can tag them as you like and also define custom properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JrFTEFL_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/aadd9a9068504ee5cec8c74b4aedf9f0/22a9b/playground-example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JrFTEFL_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/aadd9a9068504ee5cec8c74b4aedf9f0/22a9b/playground-example.png" alt="AsyncAPI Playground for schema" title="AsyncAPI Playground for schema"&gt;&lt;/a&gt;&lt;br&gt;
You can use the AsyncAPI Playground here: &lt;a href="//playground.asyncapi.io"&gt;playground.asyncapi.io&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, I have defined a simple &lt;code&gt;userCreated&lt;/code&gt; event. It has the three properties &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;userName&lt;/code&gt; and &lt;code&gt;role&lt;/code&gt;, where id is the required field. The schema definition is almost the same as the Swagger file. In the screenshot, you can see the generated example and schema view, which is easily readable and understandable.&lt;/p&gt;

&lt;h1&gt;
  
  
  Architecture at relayr
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x5JS2uDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/89270592a8ed4b7fc50291677ef6ab06/07004/relayr-architecture.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x5JS2uDK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/89270592a8ed4b7fc50291677ef6ab06/07004/relayr-architecture.png" alt="relayr architecture" title="relayr architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At relayr, we have multiple services running with Node.js and communicating using Kafka. We receive data from various client devices through MQTT. A Node.js service receives all these messages and then publishes relative Kafka messages. We wanted to validate that clients are sending the right payload, so we wrote an AsyncAPI schema for MQTT messages and also one for Kafka messages.&lt;/p&gt;

&lt;h1&gt;
  
  
  asyncapi-validator
&lt;/h1&gt;

&lt;p&gt;To validate incoming MQTT messages, I wrote the &lt;a href="https://www.npmjs.com/package/asyncapi-validator"&gt;asyncapi-validator&lt;/a&gt; with which you can validate any message against its AsyncAPI schema definition. First, you have to load the AsyncAPI schema definition using the &lt;code&gt;fromSource&lt;/code&gt; method and then you can validate any message with its key and payload using the validate method. Here is an example of validation for the schema we defined earlier for the &lt;code&gt;userCreated&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7sO4AqHS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/431b41d7c086f8670188ecb5bcc88aed/ba419/validator-example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7sO4AqHS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/431b41d7c086f8670188ecb5bcc88aed/ba419/validator-example.png" alt="validator example" title="validator example"&gt;&lt;/a&gt;&lt;br&gt;
This validation will give the below error because &lt;code&gt;id&lt;/code&gt; should be a UUID and &lt;code&gt;userName&lt;/code&gt; should be an email.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--by0wIfp---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/26562b1c6f3d31f9c25813af83660df1/8a479/error-example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--by0wIfp---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://engineering.relayr.io/static/26562b1c6f3d31f9c25813af83660df1/8a479/error-example.png" alt="error example" title="error example"&gt;&lt;/a&gt;&lt;br&gt;
We added the asyncapi-validator where we receive messages from the MQTT broker. If the messages pass the validation, we let them through. If they fail, we generate related error messages back to the device, so that the client can fix the payload. We also log the failures in our system for debugging.&lt;/p&gt;

&lt;h1&gt;
  
  
  AsyncAPI v2.0
&lt;/h1&gt;

&lt;p&gt;I started working with the AsyncAPI core team. We recently released version 2.0 of the AsyncAPI schema which has a lot of improvements. We are looking for more contributors. There is a lot of development going on around the schema definition and tooling. For details, check &lt;a href="//asyncapi.io"&gt;asyncapi.io&lt;/a&gt; or join our &lt;a href="https://asyncapi.com/slack-invite"&gt;slack&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The asyncapi-validator is also open source: &lt;a href="https://www.npmjs.com/package/asyncapi-validator"&gt;npmjs.com/package/asyncapi-validator&lt;/a&gt;. If you have any questions, feel free to reach out to me on twitter: &lt;a href="https://twitter.com/WaleedAshraf01"&gt;@waledashraf01&lt;/a&gt;&lt;/p&gt;

</description>
      <category>asyncapi</category>
      <category>opensource</category>
      <category>kafka</category>
      <category>microservices</category>
    </item>
  </channel>
</rss>
