An Introduction to Glee

Souvik De

Souvik De

·5 min read

For the last year, I have been working full-time on AsyncAPI, where I have actively taken part in building a new framework called Glee. This is the first of many blogs to come where I will be talking about Glee and all the cool stuff we can build with it. In this blog, I will introduce Glee and give a broad overview of how to get started and keep up with Glee's development.

Introduction

Glee is a powerful spec-first framework that streamlines the process of building server-side applications. By integrating your code with the spec, Glee takes care of the heavy lifting involved in creating and managing connections, allowing you to focus on the business logic that truly matters.

  • It makes sure your code, specification, and documentation are synchronized. Glee eliminates the possibility of straying from the spec, which compels you to embrace a spec-first methodology, ensuring that your API is always entirely defined and coherent. When your API evolves, it includes the specification and documentation.

  • Glee simplifies creating and maintaining connections, allowing you to concentrate solely on developing code that meets your business needs. By handling performance, scalability, resilience, and all other aspects necessary for production readiness, Glee frees you from the burden of managing these technicalities, enabling you to focus on delivering a high-quality application that meets the demands of your users.

Getting Started With Glee

Let's create a simple WebSocket API using Glee to understand its magic. We will create a simple WebSocket server that receives a current time from the client and then sends a "good morning," "good evening," or "good night," respectively.

Setting Up A Glee Project.

To work with Glee, you must install NPM and NodeJS version 10 or higher. You can just run the following commands in your terminal to check if you have both installed.

1# check if node is installed
2node -v
3# or
4node --version
5
6# check if NPM is installed
7npm -v
8# or
9npm --version
10

If you don't have any of the above tools missing, go ahead and install them.

Create a Glee project

We recommend creating a new Glee app using our official CLI, which sets up everything automatically. (You don't need to create an empty directory, create-glee-app will make one for you.) To create a project, run:

1
2asyncapi new glee 
3

To install AsyncAPI CLI either use npm or install binaries for your operating system from https://github.com/asyncapi/cli/releases

Once the process is complete, you should have a new Glee app ready for development and see these files that were made.

Glee File Structure

Define The Spec For Our API.

Glee being a spec-first framework, development starts with defining your API spec. For our case, we will define our API:

1asyncapi: 3.0.0
2info:
3  title: Greet Bot
4  version: 0.1.0
5servers:
6  websockets:
7    host: '0.0.0.0:3000'
8    protocol: ws
9channels:
10  greet:
11    address: greet
12    messages:
13      onGreet.message:
14        $ref: '#/components/messages/time'
15      subscribe.message:
16        $ref: '#/components/messages/greet'
17operations:
18  onGreet: # operationId
19    action: receive
20    channel:
21      $ref: '#/channels/greet'
22    messages:
23      - $ref: '#/components/messages/time'
24  greet.subscribe:
25    action: send
26    channel:
27      $ref: '#/channels/greet'
28    messages:
29      - $ref: '#/components/messages/greet'
30components:
31  messages:
32    time:
33      payload:
34        type: object
35        properties:
36          currentTime:
37            type: number
38          name:
39            type: string
40    greet:
41      payload:
42        type: string

This will be the specification that defines our API. In our case, it is very simple, as we will send a name and the time of the day, and our API will greet us accordingly.

One thing to note here is the operationId; this is needed and is a crucial part of Glee, as this is how we will be connecting our business logic with our spec, operationId is the name of the function that will be called every time a certain operation occurs. In our case, whenever the /greet channel received a message.

Define Our Operation Function.

Now for our case, we will add a file functions/onGreet.js and write up the logic for parsing our time and sending a response.

1export default async function (event) {
2  const { name, time } = event.payload
3  const t = new Date(time)
4  const curHr = t.getHours()
5  let response = ''
6  if (curHr < 12) {
7    response = `Good Morning ${name}`
8  } else if (curHr < 18) {
9    response = `Good Afternoon ${name}`
10  } else {
11    response = `Good Evening ${name}`
12  }
13  return {
14    reply: [
15      {
16        payload: response,
17      },
18    ],
19  }
20}

Every file in the functions folder acts as a handler to develop business logic for Glee. Each file should export an async function that receives an event parameter, where you have access to payload and server details.

Running And Testing Your Application

We will not execute the application and carry out testing with Postman to ensure that it is functioning as intended.

Now to run your Glee application, just execute the following:

1npm run dev
2# or 
3npm run start

Then open Postman and checkout the endpoint:

postman

Conclusion

So this is how easy it is to build a WebSocket API using Glee. Please do check out the Greet-Bot example code.

Glee is rapidly evolving and aims to support a variety of protocols, and while it is still in development, it currently has stable support for MQTT and WebSocket. As a team, we are eager to gather feedback from users like you to help us improve Glee and make it even better. If you are interested in trying out Glee, we would greatly appreciate it if you could test it out and share your thoughts about your experience. Your feedback will be invaluable in helping us identify areas for improvement and making Glee more user-friendly and effective. The best way to communicate with us is through GitHub Issues.