egg-jsnsq

2.0.2 • Public • Published

egg-jsnsq

NPM version build status Test coverage npm download

nsq client protocol based on nsqjs for egg framework

Install

$ npm i egg-jsnsq --save

Usage

Enable the nsqjs plugin

// {app_root}/config/plugin.js
exports.nsqjs = {
  enable: true,
  package: 'egg-jsnsq',
};

In controller/service, you can use app.nsqjs or ctx.nsqjs to get the nsqjs instance

nsqjs

  • publish(msg: Message)

    publish a message or a list of messages to the connected nsqd.

    msg.msgs is either a string, a Buffer, JSON serializable object, a list of strings / Buffers / JSON serializable objects. See nsqjs.Writer

    interface Message {
      topic: String;
      msgs: any;
    }
  • deferPublish(msg: Message, timeMs: Number)

    publish a message to the connected nsqd with delay. timeMs is the delay by which the message should be delivered.

Configuration

// {app_root}/config/config.default.js
exports.nsq = {
  reader: {
    lookupdHTTPAddresses: 'YOUR_HOST:PORT',
    sub:{
      demo:{
        topic: '',
        channel: '',
      },
    }
    // other options are the same as those of the nsqjs library
  },
  writer: {
    nsqdHost: 'YOUR_HOST',
    nsqdPort: 4150,
  },
};

see config/config.default.js for more detail.

Example

Subscriber

put your subscription code in the folder {app_root}/app/nsq and name it as the key of the sub configuration, such as demo.js

.
├── app
│   ├── nsq
│   │   └── demo.js
│   ├── public
│   └── router.js
├── config
│   └── config.default.js
├── package.json

the export of the subscription code can be a class or function/asyncFunction

  • class

    should implment a subscriber as blow

    class Subscribe {
      constructor(ctx) {
        this.ctx = ctx;
      }
    
      async subscribe(msg) {
        // enter your code
      }
    }
    
    module.exports = Subscribe;
  • function

    module.exports = async (ctx, msg) => {
      // enter your code
    };

    received msg will be anJSON Object (If it can be resolved) or Buffer

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-jsnsq

Weekly Downloads

2

Version

2.0.2

License

MIT

Unpacked Size

12.1 kB

Total Files

7

Last publish

Collaborators

  • clanaid