shimo-ali-ons

2.2.1 • Public • Published

ali-ons

NPM version build status David deps node version

Aliyun Open Notification Service Client (base on opensource project RocketMQ)

Sub module of ali-sdk.

Install

npm install ali-ons --save

Usage

consumer

'use strict';
 
const httpclient = require('urllib');
const Consumer = require('ali-ons').Consumer;
const consumer = new Consumer({
  httpclient,
  accessKey: 'your-accesskey',
  secretKey: 'your-secretkey',
  consumerGroup: 'your-consumer-group',
  // isBroadcast: true,
});
 
consumer.subscribe(config.topic, '*', function*(msg) {
  console.log(`receive message, msgId: ${msg.msgId}, body: ${msg.body.toString()}`)
});
 
consumer.on('error', err => console.log(err));

producer

'use strict';
 
const co = require('co');
const httpclient = require('urllib');
const Producer = require('ali-ons').Producer;
const Message = require('ali-ons').Message;
 
const producer = new Producer({
  httpclient,
  accessKey: 'your-accesskey',
  secretKey: 'your-secretkey',
  producerGroup: 'your-producer-group',
});
 
producer.ready(() => {
  console.log('producer ready');
  const msg = new Message('your-topic', // topic
    'TagA', // tag
    'Hello ONS !!! ' // body
  );
 
  // Might be an order id or other key you want
  const shardingKey = 'your sharding key goes here'
 
  co(function *() {
    // Send normal message, message will delivery averagely into topic queues
    let sendResult = yield * producer.send(msg);
 
    // Send sharding message
    // Messages will delivery by sharding key, that means the messages with
    // same sharding key will always be deliveried into same queue, but you
    // should handle the situation when consumers scaling yourself.  
    sendResult = yield * producer.send(msg, shardingKey);
  });
});

Rocketmq

implemention from yeliex/rocketmq

consumer

const consumer = new Consumer({
  namesrvAddr: 'localhost:9876', // for rocket mq
  accessKey: 'your-accesskey',  // for aliyun-ons
  secretKey: 'your-secretkey',  // for aliyun-ons
  onsAddr: '',                   // for aliyun-ons
  consumerGroup: 'your-consumer-group',  // for aliyun-ons
  
  isBroadcast: false, // default is false, that mean messages will be pushed to consumer cluster only once.
});

producer

const producer = new Producer({
  namesrvAddr: 'localhost:9876', // for rocket mq
  accessKey: 'your-accesskey',   // for aliyun-ons
  secretKey: 'your-secretkey',    // for aliyun-ons
  producerGroup: 'your-producer-group',  // for aliyun-ons
});

License

MIT

Dependencies (19)

Dev Dependencies (7)

Package Sidebar

Install

npm i shimo-ali-ons

Weekly Downloads

2

Version

2.2.1

License

MIT

Last publish

Collaborators

  • luoyjx