npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

polo-moleculer

v0.0.7

Published

Moleculer Mixin for Queued communication between services

Downloads

25

Readme

Polo-Moleculer Module

The Polo-Moleculer module is a Moleculer mixin to enable services to exchange asyncronous integration messages, in a request/response fashion. Currently Polo supports AWS SQS but other transports may be added in the future.

Configuration

Before using Polo-Moleculer you must have a configuration object or file with some information like credentials and sqs options. More details about this configuration can be found in Polo Messaging in GitHub.

{
  "stage":"test",
  "aws":{
    "api":{
      "accessKeyId": "foobar",
      "secretAccessKey": "foobar",
      "region": "us-east-1"
    },
    "sqs":{
      "create": true,
      "endpoint":"http://localhost:4576",
      "consume":{
        "MaxNumberOfMessages":1
      }             
    }
  }
}

Broker Middleware

Polo-Moleculer will inject actions to your service based on the messages defined (see examples bellow). To enable this injection you have to set a middleware in your broker.

const { PoloMoleculerMiddleware } = require('polo-moleculer')

var broker = new ServiceBroker({
  middlewares: [ PoloMoleculerMiddleware ]
})

Service Provider

The following example creates a service that will receive greetings requests. Notice that onRequest will be called when a new greetings message is received.

const { PoloMoleculerMixin } = require('polo-moleculer')
const sampleConfig = require('./sample_conf.json')

module.exports = {
  name: 'sample-server',
  mixins: [PoloMoleculerMixin],
  settings: {
    polo: Object.assign({app: 'Server'}, sampleConfig) // Identifies this app/service
  },

  messages: {
    greetings: {
      onRequest (ctx) {
        var message = ctx.params

        // Reads message's content and send a reply
        return message.reply({answer: `Hello ${message.body.name}`})
      }
    }
  }
}

Service Consumer

To consume a service you'll need another service (likely in a remote app) that will send a greetings message request and receive its response.

Polo-Moleculer will automatically create a send<Message>Request action that can be used to send a request to server (see Using Service Consumer). )

const { PoloMoleculerMixin } = require('../src')
const sampleConfig = require('./sample_conf.json')

module.exports = {
  name: 'sample-client',
  mixins: [PoloMoleculerMixin],
  settings: {
    polo: Object.assign({app: 'Client'}, sampleConfig)
  },

  messages: {
    greetings: {
      target: 'Server', // Name of the target app (same defined in server's settings)

      onResponse (ctx) {
        var message = ctx.params
        // Could get some information from the message
        console.log(message.body.answer) // should print 'Hello <name>'

        return message.done()
      }
    }
  }
}

Using Service Consumer

Once you have both services created, you can send messages to the server, through the client, like this:

broker.call('sample-client.sendGreetingsRequest', {
  body: {
    name: 'Moleculer'
  }
})
  .then(receipt => console.log('Message sent !'))

Receiving messages

All Polo-Moleculer services have a receiveMessages action that will load messages from the queue and start processing it (that is, call onRequest or onResponse actions). The number of messages read or waiting period can be set in the Configurations.

broker.call('sample-server.receiveMessages')
  .then(numOfMessages => console.log(numOfMessages, 'were read/processed'))

You can also configure service to periodically read messages using a cron-like expression in settings:

module.exports = {
  name: 'sample-server',
  mixins: [PoloMoleculerMixin],
  settings: {
    polo: Object.assign({app: 'Server'}, sampleConfig)
    readScheduling: '*/15 * * * * *', // checks queue every 15s
    autoRead = true // Automatically starts scheduling when service is created
  },
  ...

You can set autoRead to automatically start scheduler once service is started. You can also start/stop scheduler by calling actions startSchedule and stopSchedule, respectively.