Preparing for Installation
Before starting with Glee, ensure that NPM, Node.js, and the AsyncAPI CLI are already set up on your system.
Need help installing AsyncAPI CLI? Check out the CLI Installation Guide.
Starting with Glee
Creating a new Glee application can be approached in three distinct ways:
- Begin with an existing AsyncAPI document.
- Use a predefined template.
- Opt for Manual Installation (recommended for advanced users keen on understanding Glee's inner workings).
Initialize with an AsyncAPI Document
If you already have an AsyncAPI specification file, you can jumpstart your project by using the -f
or --file
flag to specify the file path. The CLI will leverage this to set up your project.
asyncapi new glee -f asyncapi.yaml
This command generates all necessary files. Just open the project in your editor, and start integrating your authentication and business logic.
Start with a Template
For those without a custom AsyncAPI file and looking to experiment, starting with a template is an excellent choice. These are simple Glee projects that the AsyncAPI CLI can generate for you.
Use the -t
or --template
flag to specify the template name. Currently, two templates are available:
- default: Responds with a string upon receiving a string.
- tutorial: Designed for this tutorial.
To initiate a project with the default template:
asyncapi new glee -t default
After installation, follow these steps:
1Next steps:
2
3 cd project
4 npm install --ignore-scripts
5 npm run dev
6
7Feel free to open the project in your preferred editor and start customizing it.
Refer to our Getting Started guide for further assistance during customization.
Manual Installation
For a hands-on setup, start by creating a new directory, e.g., myapp
, and structure it as follows:
1├─ functions (required)
2│ ├─ onHello.js
3│ └─ ...
4├─ lifecycle (optional)
5│ ├─ onConnect.js
6│ └─ ...
7├─ .env (optional)
8├─ asyncapi.(yaml | yml | json) (required)
9├─ glee.config.js (optional)
10├─ package.json (required)
Inside this new folder, initialize and install Glee:
1npm init -y
2npm install @asyncapi/glee
Edit your package.json
to include the following scripts:
1{
2 "scripts": {
3 "docs": "glee docs",
4 "dev": "glee dev",
5 "start": "glee start",
6 }
7}
These scripts serve different development stages:
glee docs
: Generates your project documentation.glee dev
: Starts a local development server, building your project in a development-friendly environment.glee start
: Launches your project for production use.
Setting up asyncapi.yaml
and Required Directories
Craft an asyncapi.yaml
file capable of receiving a "hello {name}" message via WebSocket (ws
) protocol on the hello
channel. Define the operation ID as onHello
, indicating the function to be called, and set the payload type to string for publishing on that channel.
1# AsyncAPI Specification for Hello, Glee!
2asyncapi: 3.0.0
3info:
4 title: 'Hello, Glee!'
5 version: 1.0.0
6servers:
7 websockets:
8 host: 0.0.0.0:3000
9 protocol: ws
10channels:
11 hello:
12 address: hello
13 messages:
14 hello:
15 $ref: '#/components/messages/hello'
16operations:
17 onHello:
18 action: receive
19 channel:
20 $ref: '#/channels/hello'
21 SendHello:
22 action: send
23 channel:
24 $ref: "#/channels/hello"
25components:
26 messages:
27 hello:
28 payload:
29 type: string
Create a function onHello.js
in myapp/functions
:
1export default async function (event) {
2 return {
3 send: [{
4 server: "websockets",
5 channel: "hello",
6 payload: `Hello from Glee! You said: "${event.payload}".`
7 }]
8 }
9}
Launching the Development Server
- Execute
npm run dev
to start the server. - Connect to
ws://localhost:3000/hello
and send a WebSocket request like{"john"}
.