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 events from the Service to the Engine, the Service has to follow the Protobuffer definition and use gRPC.

TIP

Consider emitting event when the service is ready. If the service needs to synchronize data first, it should wait for the synchronization to complete before emitting events.

Service.EmitEvent

Name Type Required Description
token String Required The token given by the Engine as environment variable MESG_TOKEN
eventKey String Required The event's key defined in the service file
eventData String Required The event's data in JSON format
{
    "token": "TOKEN_FROM_ENV",
    "eventKey": "eventX",
    "eventData": "{\"foo\":\"hello\",\"bar\":false}"
}

Reply of EmitEvent API doesn't contain any data.

Examples

const mesg = require('mesg-js').service()

mesg.emitEvent("eventX", {
  foo: "hello",
  bar: false,
}).catch((err) => {
  console.error(err)
})

See the MESG.js library for additional documentation

package main

import (
	"github.com/mesg-foundation/core/client/service"
)

type EventX struct {
	Foo string `json:"foo"`
	Bar bool   `json:"bar"`
}

func main() {
	s, _ := service.New()

	s.Emit("eventX", EventX{
		Foo: "hello",
		Bar: false,
	})
}

See the Go Service package for additional documentation

Get Help

You need help ? Check out the MESG Forum.

Last Updated: 5/20/2019, 6:03:39 AM