# Emit an Event

# Why emit an Event?

Events are emitted from a Service (e.g.: a web server receiving a request, or a blockchain technology receiving a new transaction). These events are emitted to achieve a desired effect or to be used as a trigger to make another task happen. Each Service has different kinds of events that you can send to the Engine.

# Steps to follow

To emit events from your Service, you'll need to:

# Event definitions

To create an event, the first step is to update the Service's mesg.yml file and add an event indexed by its key with the following attributes:

Attribute Type Default Description
name String id Name of the event. If the name is not set, it will be the same as the ID you choose for the event.
description String "" Describe the event, what's its purpose is and why users would want to use it.
data map<id,Data> {} The structure of the event's data.

# Event's data

Attribute Type Default Description
name String id Name of the data
description String "" Description of the data
type Type String Type of data
object Data {} Nested data. Data can contain child data. It can only be defined when type is Object
optional Boolean false Mark the data as optional
repeated Boolean false Define this data as an array of the type selected

# Data's type

The data type can be one of the following:

  • String
  • Boolean
  • Number
  • Object
  • Any

# Example

Example of an event definition in a mesg.yml file:

...
events:
  eventX:
    name: "Event X"
    description: "This is event X"
    data:
      foo:
        name: "Foo"
        description: "Foo is a string"
        type: String
        optional: false
      bar:
        name: "Bar"
        description: "Bar is an optional array of boolean"
        type: Boolean
        optional: true
        repeated: true
...

# Emit an Event

To emit an event, the service should call the mesg.emitEvent function with the event's key and event's data as parameters. This function returns a Promise.

const Service = require('@mesg/service')
const mesg = new Service()
mesg.emitEvent("eventX", {
  foo: "hello",
  bar: false,
})

See the @mesg/service library for additional documentation

API definition

To see the API definition, check the Event API definition.

Get Help

You need help ? Check out the MESG Forum.