Introduction
In this tutorial, you'll learn how to generate an application that uses the Glee framework. You'll do it with an AsyncAPI document and the AsyncAPI CLI.
Background context
Glee is a TypeScript/JavaScript framework that enables you to create APIs and messaging clients based on your AsyncAPI document. Instead of generating code, this framework tightly integrates with your AsyncAPI document and binds functions to specific AsyncAPI operations. You only have to provide the code for these functions and Glee handles the rest.
Glee is often used with the AsyncAPI CLI for a better development experience.
In the previous tutorial, you created an AsyncAPI document that is used in this tutorial.
Remember
If you did not follow the previous tutorial and do not have an asyncapi.yaml
file for overview, then generate one by running the following command using the AsyncAPI CLI:
asyncapi new --example=tutorial.yml --no-tty
.
Installation guide
Remember
It is important to have Node.js installed which will enable the installation of the required dependencies using npm.
CLI Installation
Follow the AsyncAPI CLI installation instructions below, based on your computer’s operating system.
brew
You can install in MacOS via brew: brew install asyncapi
.
pkg
Each release of CLI produces a MacOS dedicated pkg file that enables you to install this CLI as MacOS application.
1# Download latest release. To download specific release, your link should look similar to https://github.com/asyncapi/cli/releases/download/v0.13.0/asyncapi.pkg. All releases are listed in https://github.com/asyncapi/cli/releases
2curl -OL https://github.com/asyncapi/cli/releases/latest/download/asyncapi.pkg
3# Install AsyncAPI CLI
4sudo installer -pkg asyncapi.pkg -target /
You can install in Linux via dpkg
, a debian package manager:
curl -OL https://github.com/asyncapi/cli/releases/latest/download/asyncapi.deb
sudo dpkg -i asyncapi.deb
For Windows, install the appropriate installer and follow the default installation steps to complete the installation process.
Download asyncapi.x64.exe for 64-bit Windows and asyncapi.x86.exe for 32-bit Windows.
Read further AsyncAPI CLI installation instructions for different operating systems.
Using NPM and Node.js
Alternitavely, you can install the AsyncAPI CLI with Node.js >=v10
and NPM.
Install AsyncAPI CLI globally with the following command:
npm install -g @asyncapi/cli
To install a specific version of the AsyncAPI CLI, pass the verion
during installation:
npm install -g @asyncapi/cli@{version}
Create a Glee project
-
Trigger the creation of the Glee project:
1asyncapi new glee --name=tutorial --template tutorial
Let's break down the previous command:
asyncapi new glee
is how you use Glee via the AsyncAPI CLI.--name=tutorial
is how you tell the AsyncAPI CLI to name your new Glee project.--template=tutorial
is how you tell the AsyncAPI CLI to use the template of a Glee project that was created specifically for this tutorial.
-
List all files in the directory and confirm your Glee project creation:
1cd tutorial && ls
Upon execution of the command above, the following is an example of the expected result:
1$ ls 2 LICENSE 3 README.md 4 asyncapi.yaml 5 functions 6 package.json
Start generated application
-
Install dependencies of the newly generated application:
1npm install
-
Start the application:
1npm run dev
Send message to broker
-
In another terminal install the MQTT.js library:
1npm install mqtt -g
-
Send a message to the MQTT broker that's connected to your generated application. Run the following MQTT client command:
1mqtt 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 to check if your application logged the streetlight condition you just sent. You should see something like this displayed in the terminal:
1lightMeasured was received from mosquitto: 2 { id: 1, lumens: 3, sentAt: '2017-06-07T12:34:32.000Z' } 3 Streetlight with id "1" updated its lighting information to 3 lumens at 2017-06-07T12:34:32.000Z.
Summary
In this tutorial, you learned how to create a Glee project from the Streetlights API specification document created in a previous tutorial.
Additionally, you've learned how to run your code by installing the project's dependencies and sending several test messages to the Streelights application using the MQTT client.
Next steps
Now that you've completed this tutorial, go ahead and learn how to validate your AsyncAPI messages (events) through the message validation techniques supported by AsyncAPI.