Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve kafka bindings #678

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/pages/env-vars-config.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert the changes to the docs? since this PR is about KAFKA bindings I don't think it's appropriate for it to include any other changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR is old, I had to merge it with the current master branch, so there isn't any change that's not already on the master branch ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oviecodes
If Diff shows there are, then there are changes that this PR is proposing.
for example. master doesn't have this file: docs/pages/glee-template.md since it was removed by a previous PR.
but you are adding it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh Damn, I'm just pulling from the master branch. I'll try updating from master again, if it doesn't work then I'll scrap this PR, it seems it's too old and things are getting a bit more complicated.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@ Glee provides several environment variables that allow you to tailor your applic
|GLEE_SERVER_CERTS|Indicates server-specific certificate files in a `${serverName}:${pathToCertificateFile}` format, separated by commas.|`GLEE_SERVER_CERTS=mosquitto:mosquitto.org.crt`|
|GLEE_SERVER_VARIABLES|Sets server variables in a `${serverName}:${serverVariable}:${value}` format, separated by commas.|`GLEE_SERVER_VARIABLES=websockets:namespace:public`|

<<<<<<< HEAD
### Handling Multiple .env Files
Glee supports loading variables from `.env.local` directly into `process.env`. This feature is handy for keeping secrets out of your repository during development. You can also set environment-specific defaults in `.env.development` or `.env.production`.
=======
### Support for multiple .env files
Glee has support for loading variables from `.env.local` into `process.env`.
This is useful for storing secret environment variables needed in development while keeping them out of the repository.
However, sometimes you might want to add some defaults for the `development` or `production` environment. You can do that by creating files with the following names:
`.env.development` or `.env.production`

`.env.local` always overrides any other existing `.env*` file.

You can change the environment by setting the `NODE_ENV` variable to `development` or `production`.

## Configuring Glee
>>>>>>> 1f51e1f (feat: add support for multiple .env files. (#683))

`.env.local` takes precedence over other `.env*` files.

Expand Down
10 changes: 5 additions & 5 deletions docs/pages/function-lifecycle-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ Functions take a single argument, which is the event received from a broker or a
|channel|The name of the channel/topic from which the event was read.|
|serverName|The name of the server/broker from which the event was received.|

Functions may return an object to instruct Glee on what action to take next. For instance, the following example sends a greeting message to the `development` server:
Functions may return an object to tell Glee what to do next. For instance, the following example sends a greeting message to `development` server:
```js
/* onHello.js */
export default async function (event) {
return {
send: [{
server: 'development',
server: 'developement',
channel: 'greets',
payload: 'Greetings! How is your day going?'
}]
Expand All @@ -37,10 +37,10 @@ export default async function (event) {
```

|Attribute|Type|Description|
|---------|----|-----------|
|send|array&lt;[OutboundMessage](#anatomy-of-an-outbound-message)&gt;|A list of outbound messages to send after processing the inbound event. All clients subscribed to the given channel/topic will receive the message.
|---|---|---|
|send|array&lt;[OutboundMessage](#anatomy-of-an-outbound-message)&gt;|A list of outbound messages to send when the processing of the inbound event has finished. All clients subscribed to the given channel/topic will receive the message.

##### Anatomy of an Outbound Message
##### Anatomy of an outbound message
|Attribute|Type|Description|
|---------|----|-----------|
|payload|string|The payload/body of the message you want to send.|
Expand Down
98 changes: 98 additions & 0 deletions docs/pages/glee-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: "Create AsyncAPI Glee template"
weight: 30
---
This tutorial teaches you how to create a simple glee template. You'll use the AsyncAPI Glee template that you develop to generate Javascript code. Additionally, you'll create a template code with a reusable component to reuse the custom functionality you create and test your code using an WS server.

<CodeBlock>
{`asyncapi: 3.0.0
info:
title: 'Hello, Glee!'
version: 1.0.0
servers:
websockets:
host: 0.0.0.0:3000
protocol: ws
channels:
hello:
address: hello
messages:
hello:
$ref: '#/components/messages/hello'
operations:
onHello:
action: receive
channel:
$ref: '#/channels/hello'
SendHello:
action: send
channel:
$ref: "#/channels/hello"
components:
messages:
hello:
payload:
type: string`}
</CodeBlock>

Let's break it down into pieces:

<CodeBlock>
{`info:
title: 'Hello, Glee!'
version: 1.0.0`}
</CodeBlock>

The `info` section provides general information about the API, including its title and version.

Moving on, let's talk about the `servers` section.

<CodeBlock>
{`servers:
websockets:
host: 0.0.0.0:3000
protocol: ws`}
</CodeBlock>

The servers section defines the different servers where the API can be accessed. In this case, there is a single server named "websockets" that uses the WebSocket protocol (`ws`) and listens on the address `ws://0.0.0.0:3000`.

Now lets move on to the `channels` section. This section is used to describe the event names your API will be publishing and/or subscribing to.

<CodeBlock>
{`channels:
hello:
address: hello
messages:
hello:
$ref: '#/components/messages/hello'
operations:
onHello:
action: receive
channel:
$ref: '#/channels/hello'
sendHello:
action: send
channel:
$ref: '#/channels/hello'`}
</CodeBlock>

The channels section defines the communication channels available in the API. In this case, there's a channel named "hello". This channel supports both sending and receiving.

The `receive` action indicates that messages received on the `hello` channel should follow the structure defined in the hello message component.
The `send` action specifies that the operation with ID `sendHello` is used for sending messages to the `hello` channel. The message structure is referenced from the hello message component. The message payload is going to be validated against the `sendHello` operation message schemas.

Next is the `payload` property under `hello` message component which is used to understand how the event should look like when publishing to that channel:

<CodeBlock>
{`components:
messages:
hello:
payload:
type: string`}
</CodeBlock>

The components section contains reusable elements, in this case, a definition for the "hello" message. It specifies that the payload of the "hello" message should be of type string.

## Summary

In this tutorial, you learned how to create an AsyncAPI specification document via a simple example with a glee template.
112 changes: 112 additions & 0 deletions docs/pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,115 @@ Glee expects your project to have some files and folders with special names. Whe
|package.json|**Required.** The Node.js package definition file. Make sure you include `@asyncapi/glee` as a dependency and add two scripts: `dev` and `start`. They should be running `glee dev` and `glee start` respectively.

To understand the structure in a broader way, please refer to the associated page's links.

### Let's create a glee project to simplify the app structure

We will consider 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 send a "good morning", "good evening" or "good night" respectively.

To setup a project, you should follow our installation page on how to setup glee on your environment.

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: `asyncapi new glee`

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

#### Define our Spec for our API

Glee being a spec-first framework, development starts with defining your API spec. To know more details into it, you can follow glee template to understand it step by step. For our case we will define our API:

```yaml
asyncapi: 3.0.0
info:
title: Greet Bot
version: 1.0.0
servers:
websockets:
host: 0.0.0.0:3000
protocol: ws
channels:
greet:
address: greet
messages:
greet:
$ref: '#/components/messages/greet'
time:
$ref: '#/components/messages/time'
time:
address: time
messages:
time:
$ref: '#/components/messages/time'
operations:
onGreet:
action: receive
channel:
$ref: '#/channels/greet'
sendGreet:
action: send
channel:
$ref: '#/channels/time'
components:
messages:
time:
payload:
type: object
properties:
currentTime:
type: number
name:
type: string
greet:
payload:
type: string

```

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

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

#### Define our operation function

Now for our case, we will be adding a file `functions/onGreet.js` and writing up the logic for parsing our time and sending a response.

```javascript
export default async function (event) {
const { name, time } = event.payload
const t = new Date(time)
const curHr = t.getHours()
let response = ''
if (curHr < 12) {
response = `Good Morning ${name}`
} else if (curHr < 18) {
response = `Good Afternoon ${name}`
} else {
response = `Good Evening ${name}`
}
return {
send: [
{
server: "websockets",
channel: "greet"
payload: response,
},
],
}
}

```

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

#### Running and testing our application

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

Now to execute our glee application, just run:

```
npm run dev
# or
npm run start
```
To send a WebSocket request with a payload e.g. `{"name":"john", "time": "1567906535"}` to `ws://localhost:3000/greet`, open Postman and checkout the endpoint:

So, this is how easy it is to build a WebSocket API using Glee. You can also check out the example code [here](https://github.com/Souvikns/greet-bot).
2 changes: 1 addition & 1 deletion docs/reference/classes/adapters_cluster_redis.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1429,4 +1429,4 @@ v15.4.0

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/adapters_http_client.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1650,4 +1650,4 @@ v15.4.0

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/adapters_kafka.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1537,4 +1537,4 @@ v15.4.0

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/adapters_mqtt.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1678,4 +1678,4 @@ v15.4.0

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/adapters_ws_client.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1561,4 +1561,4 @@ v15.4.0

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/adapters_ws_server.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1998,4 +1998,4 @@ v15.4.0

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/errors_glee_error.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@ Error.captureStackTrace

#### Defined in

node_modules/@types/node/globals.d.ts:4
node_modules/@types/node/globals.d.ts:4
2 changes: 1 addition & 1 deletion docs/reference/classes/lib_adapter.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1519,4 +1519,4 @@ EventEmitter.setMaxListeners

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/lib_cluster.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1370,4 +1370,4 @@ EventEmitter.setMaxListeners

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/lib_glee.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1596,4 +1596,4 @@ EventEmitter.setMaxListeners

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/lib_message.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1784,4 +1784,4 @@ EventEmitter.setMaxListeners

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317
2 changes: 1 addition & 1 deletion docs/reference/classes/lib_wsHttpAuth.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -1407,4 +1407,4 @@ EventEmitter.setMaxListeners

#### Defined in

node_modules/@types/node/events.d.ts:317
node_modules/@types/node/events.d.ts:317