Functions and Lifecycle Events

Found an error? Have a suggestion?Edit this page on GitHub

Functions

Glee relies on functions to execute your business logic. Functions are files that export a default async Node.js function:

1/* onHello.js */
2export default async function (event) {
3  // Your business logic here...
4}

Functions take a single argument, which is the event received from a broker or a client, depending on the type of API you're building. The event argument has the following structure:

AttributeDescription
payloadThe payload/body of the received event.
headersThe headers/metadata of the received event.
channelThe name of the channel/topic from which the event was read.
serverNameThe 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:

1/* onHello.js */
2export default async function (event) {
3  return {
4    send: [{
5      server: 'development',
6      channel: 'greets',
7      payload: 'Greetings! How is your day going?'
8    }]
9  };
10}
AttributeTypeDescription
sendarray<OutboundMessage>A list of outbound messages to send after processing the inbound event. All clients subscribed to the given channel/topic will receive the message.
Anatomy of an Outbound Message
AttributeTypeDescription
payloadstringThe payload/body of the message you want to send.
headersobject<string,string>The headers/metadata of the message you want to send.
channelstringThe channel/topic to which you want to send the message. Defaults to event.channel, i.e., the same channel as the received event.
serverstringThe server/broker to which you want to send the message. Defaults to event.serverName, i.e., the same server as the received event.

How Does Glee Determine Which Function to Execute?

Glee reads your asyncapi.yaml file and looks for all the receive actions containing an operations attribute field. The operations field serves as a mechanism to bind a specific operation to a function file. For instance, given the following AsyncAPI definition:

1...
2operations:
3  onHello: # operation ID
4    action: receive
5    channel:
6      $ref: '#/channels/hello'
7      ...

Glee maps the onHello operation to the functions/onHello.js file.

Lifecycle Events

Glee allows you to bind incoming messages to functions. However, sometimes it's necessary to initiate communication proactively, not merely as a reaction to another message. Use cases can vary widely: from sending a message to announce that our client is connected, to broadcasting messages at regular intervals.

To subscribe to a lifecycle event, create a file under the lifecycle directory. It must be structured as follows:

1export default async function ({
2  glee,
3  serverName,
4  server,
5  connection,
6}) {
7  // Your business logic here...
8}
9
10export const lifecycleEvent = 'onConnect'

Each file in the lifecycle directory must export a default async function and the lifecycleEvent field, indicating the name of the event you wish to subscribe to. Optionally, your function can return an object following the same syntax as described above in the functions definition.

List of Events

EventDescription
onConnectA connection with a broker has been established.
onReconnectGlee reconnected to a broker.
onDisconnectA connection with a broker has been closed.
onServerReadyYour Glee server is now ready to accept connections.
onServerConnectionOpenA client has opened a connection with your Glee server.
onServerConnectionCloseA client has closed the connection with your Glee server.

All of them take a single argument containing information about the event:

AttributeDescription
gleeA reference to the Glee app.
serverNameThe name of the server where the event occurred.
serverThe AsyncAPI definition of the server where the event occurred.
connectionThe connection where the event occurred.

Restricting the Lifecycle Event

In some cases, it's useful to restrict the lifecycle event to a specific server or set of servers. To do this, add a line like the following to your lifecycle file:

export const servers = ['mosquitto']

The above example ensures Glee fires the lifecycle event only if it originates from the mosquitto server.

Similarly, you may want to restrict the lifecycle event to a specific channel/topic. Add a line like this to your lifecycle file:

export const channels = ['user/signedup']

The above example ensures Glee fires the lifecycle event only if the connection includes the channel user/signedup.

Was this helpful?
Help us improve the docs by adding your contribution.
OR
Github:AsyncAPICreate Issue on GitHub