Message
Events traversing layline.io Workflows are instantiated as a Message. This class exposes a number of properties and methods to extract and set data within messages.
To understand the anatomy of a message please read the respective chapter in the documentation.
Example Message Structure
Assume we have the following data dictionary structure
- Header
- IOT
- RECORD_TYPE
- DEVICE_NO
- IOT
- Detail
- IOT
- RECORD_TYPE
- TIME
- MEASUREMENT
- IOT
- Trailer
- IOT
- RECORD_TYPE
- COUNT
- IOT
Then in a Javascript processor we can do this:
...
export function onMessage() {
if (message.typeName === 'Header') {
onHeader (message);
} else if (message.typeName === 'Detail') {
onDetail(message);
} else if (message.typeName === 'Trailer') {
onDetail(message);
}
// send the message on through OUTPUT_PORT of Processor
stream.emit(message, OUTPUT_PORT);
}
...
And this:
...
// Handle a detail record type
function onDetail (message) {
const m = message.data.IOT.MEASUREMENT;
const VENDOR = Status.getVendorByName('MyVendorLongName');
if (m < 0) {
message.addStatus(Severity.ERROR, Status.create(VENDOR, 'ILLEGAL_MEASUREMENT', m));
}
}
...
Properties
data
data:
Object
The data of the message. It is a nested object structure that reflects the structure of the data dictionary of this message.
Example
// Create Detail
const detailMessage = dataDictionary.createMessage(dataDictionary.type.Detail);
detailMessage.data.PRODUCT = {
RECORD_TYPE : "D",
ID : message.data.Id,
CODE : message.data.Code,
NAME : message.data.Name,
CATEGORY : message.data.Category,
PRICE : message.data.Price,
STOCK_QUANTITY : message.data.StockQuantity,
COLOR : message.data.Color,
LAUNCH_DATE : message.data.LaunchDate,
}
// stream.logInfo(f"detailMessage: {detailMessage.toJson()})")
stream.emit(detailMessage, OUTPUT_PORT);
id
id:
string
The unique identifier of the message. This is a consecutive number starting with "1" for the first message. It is used to uniquely identify a message within a stream. Cloning a message will generate a new id, whereas the original message will keep its id and the cloned message will have the original message number appended by a "." and a new consecutive number. For example, "1.1", "1.2", "1.3", ... for each clone of the original message.
Example
// Accessing the id of a message
const id = message.id;
typeName
typeName:
string
The type name of the message. This is the name of the data dictionary type that the message represents.
Example
const typeName = message.typeName;
// e.g, If in your data dictionary you have a type called "MyType", then this will return "MyType"
if (message.typeName === 'MyType') {
// do something
}