Socket
Socket
Sign inDemoInstall

mesg-js

Package Overview
Dependencies
145
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mesg-js

[Website](https://mesg.com/) - [Docs](https://docs.mesg.com/) - [Forum](https://forum.mesg.com/) - [Chat](https://discordapp.com/invite/SaZ5HcE) - [Blog](https://medium.com/mesg)


Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
2
Install size
58.3 MB
Created
Weekly downloads
 

Readme

Source

mesg-js

Website - Docs - Forum - Chat - Blog

mesg-js is the official JavaScript library to interact with MESG Engine.

This library can be used from an Application or a Service.

Status

CircleCI codecov

Contents

Installation

npm i mesg-js

Service

Require mesg-js as a service:

const { service } = require('mesg-js')

const mesg = service()

Task

The service have to call mesg.listenTask to start listening for task to execute by passing an object containing the tasks' key and function.

mesg.listenTask({
  'TASK_1_KEY': (inputs) => {
    // Function of the task 1
    // Can directly throw error
    if (inputs.foo === undefined) {
      throw new Error('foo is undefined')
    }
    // Return an object
    return { foo: inputs.a + 'bar' }
  }, 
  'TASK_2_KEY': async (inputs) => {
    // Function of the task 2
    // Return an Promise containing an object
    const response = await fetch('...')
    return response.json()
  },
})

The task functions accept inputs as parameter and returns the outputs as object or Promise of object. The task function can throw an Error in case of error. The lib will catch it and send it to the MESG Engine.

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.

mesg.emitEvent('EVENT_KEY', { foo: 'bar' })

Application

Require mesg-js as an application:

const { application } = require('mesg-js')

const mesg = application()

MESG Engine endpoint

By default, the library connects to the MESG Engine from the endpoint localhost:50052.

Resolve SID

Instead of hard-coding instanceHash in your application's env, your application can resolve dynamically using the service's SID.

const instanceHash = await mesg.resolve('SID_OF_THE_SERVICE')

const result = await mesg.executeTaskAndWaitResult({
  instanceHash,
  .....
})

Listen events

Listen events from a service.

const instanceHash = await mesg.resolve('SID_OF_THE_SERVICE')

mesg.listenEvent({
  filter: {
    instanceHash: instanceHash,
    key: 'EVENT_KEY' // optional
  }
})
.on('data', (event) => {
  console.log('an event received:', event.key, mesg.decodeData(event.data))
})

Listen results

Listen results from a service.

const instanceHash = await mesg.resolve('SID_OF_THE_SERVICE')

mesg.listenResult({
  filter: {
    instanceHash: instanceHash,
    taskKey: 'TASK_KEY_FILTER', // optional
    tags: ['TAG_FILTER'] // optional
  }
})
.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 instanceHash = await mesg.resolve('SID_OF_THE_SERVICE')

const execution = await mesg.executeTask({
  instanceHash: instanceHash,
  taskKey: 'TASK_KEY',
  inputs: mesg.encodeData({ key: 'INPUT_DATA' }),
  tags: ['ASSOCIATE_TAG'] // optional
})
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 instanceHash = await mesg.resolve('SID_OF_THE_SERVICE')

const result = await mesg.executeTaskAndWaitResult({
  instanceHash: instanceHash,
  taskKey: 'TASK_KEY',
  inputs: mesg.encodeData({ key: 'INPUT_DATA' }),
  tags: ['ASSOCIATE_TAG'] // optional
})
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))

Community

You can find us and other MESG users on the forum. Feel free to check existing posts and help other users of MESG.

Also, be sure to check out the blog to stay up-to-date with our articles.

Contribute

Contributions are more than welcome.

If you have any questions, please reach out to us directly on Discord.

FAQs

Last updated on 25 Oct 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc