@mesg/application
Website - Docs - Forum - Chat - Blog
This library lets you connect to the MESG engine to listen for any event or result that you might be interested too. It also allows you to execute a task, either synchronously or asynchronously.
Contents
Installation
npm install @mesg/application
Application
Require mesg-js as an application:
const Application = require('@mesg/application')
const mesg = new Application()
MESG Engine endpoint
By default, the library connects to the MESG Engine from the endpoint localhost:50052
.
Resolve SID
Instead of hard-coding runnerHash
in your application's env, your application can resolve dynamically using the service's SID.
const runnerHash = await mesg.resolveRunner('SID_OF_THE_SERVICE')
const result = await mesg.executeTaskAndWaitResult({
executorHash: runnerHash,
.....
})
Listen events
Listen events from a service.
const instanceHash = await mesg.resolve('SID_OF_THE_SERVICE')
mesg.listenEvent({
filter: {
instanceHash: instanceHash,
key: 'EVENT_KEY'
}
})
.on('data', (event) => {
console.log('an event received:', event.key, mesg.decodeData(event.data))
})
Listen results
Listen results from a service.
const runnerHash = await mesg.resolveRunner('SID_OF_THE_SERVICE')
mesg.listenResult({
filter: {
executorHash: runnerHash,
taskKey: 'TASK_KEY_FILTER',
tags: ['TAG_FILTER']
}
})
.on('data', (result) => {
if (result.error) {
console.error('an error has occurred:', result.error)
return
}
console.log('a result received:', mesg.decodeData(result.outputs))
})
Execute task
Execute task on a service.
const runnerHash = await mesg.resolveRunner('SID_OF_THE_SERVICE')
const execution = await mesg.executeTask({
executorHash: runnerHash,
taskKey: 'TASK_KEY',
inputs: mesg.encodeData({ key: 'INPUT_DATA' }),
tags: ['ASSOCIATE_TAG']
})
console.log('task in progress with execution:', execution.hash)
Execute task and wait result
Execute task on a service and wait for its result.
This can be considered as a shortcut for using both executeTask()
and listenResult()
at same time.
const runnerHash = await mesg.resolveRunner('SID_OF_THE_SERVICE')
const result = await mesg.executeTaskAndWaitResult({
executorHash: runnerHash,
taskKey: 'TASK_KEY',
inputs: mesg.encodeData({ key: 'INPUT_DATA' }),
tags: ['ASSOCIATE_TAG']
})
if (result.error) {
console.error('an error has occurred:', result.error)
throw new Error(result.error)
}
console.log('a result received:', mesg.decodeData(result.outputs))