Since the ONS SDK of node.js has not been released in aliyun (there is nothing in the master branch, it is in another branch), I used the C++ SDK of ONS to create a node.js version myself.

In fact, I also thought about contributing code to the official, but after a rough glance, I found that the agreement of this product has no document to write down the situation will cost a lot of energy, I can only use the existing SDK for opportunistic masturbating.

In fact, this package was written a month ago, but I didn’t find a good way to support ACK at that time. Today, it suddenly came to my mind that I used Libuv’s black technology to solve the support for ACK, and I just moved it to the stage and sent it out.

But there is one caveat:

Since the official C++ SDK of aliyun ONS is a closed source and only provides a static link library (libonsclient4cpp.a) under Linux, the package can only be installed under Linux so far.

To develop an environment, OSX users should move to Linux or start a Vagrant, Docker, etc.

But when ali cloud’s C++ SDK compiles the OSX link library, I will support it immediately.

XadillaX/ Aliyun-ons: SDK of Node.js for Ali…

ONS (Open Messaging Service) is a cloud messaging product based on MetaQ (RocketMQ), alibaba’s open messaging middleware.

The installation

$ npm install --save ons

Note: since the official C++ SDK of aliyun ONS is closed source and only provides a static link library (libonsclient4cpp.a) under Linux, the package is currently only supported under Linux.

To develop an environment, OSX users should move to Linux or start a Vagrant, Docker, etc.

In addition, since the C++ SDK needs to feed back the processing results for synchronous execution in the thread, while node.js needs to be executed asynchronously, it cannot feed back the results in time. This package can only allow all messages to be processed successfully, that is, the ACK status. (The ACK success failure feature has been coded!)

Welcome to provide solutions and optimizations.

Method of use

First you need to open ONS service and get access key and Secret key, then create a consumer ID or producer ID, and topic.

For details, please refer to ali Cloud ONS help or Ali Cloud console.

The sample

You can refer to two sample files consumer.js and producer.js.

Consumer

Create a Consumer with the following code.

var Consumer = require("ons").Consumer;
var consumer = new Consumer(CUSTOMER_ID, TOPIC, TAGS, ACCESS_KEY, SECRET_KEY);Copy the code

Then create an event listener to get the message.

Consumer.on ("message", function(MSG, ack) {// Do something // // This function is triggered when a message is received. // // Don't forget to call 'ack.done(true)' or 'ack.done(false)' // to tell ONS that you handled the message successfully or failed, If fail the ONS will retry / / / / ` ack. Done () ` equivalent to ` ack. Done (true) `});Copy the code

Once you have created and set up the listener, you can initialize the Consumer and start listening for messages.

consumer.init(function(err) {
    if(err) return console.log(err);
    consumer.listen();
});Copy the code

Also, you can stop it when you want.

consumer.stop();Copy the code

Producer

Create a Producer using the following code.

var Producer = require("ons").Producer;
var producer = new Producer(PRODUCER_ID, ACCESS_KEY, SECRET_KEY);Copy the code

After it is created, you need to start it to send messages.

producer.start(function(err) { if(err) return console.log(err); console.log("Started!" ); });Copy the code

You can then send messages using the send function.

producer.send(KEY, TOPIC, TAGS, CONTENT, function(err, messageId) { console.log(arguments); }); // The 'KEY' argument is not mandatory, so you can also call producer.send(TOPIC, TAGS, CONTENT, function(err, messageId) {console.log(arguments); });Copy the code

Of course, you can stop it whenever you want.

producer.stop();Copy the code

Contribute

Come on come on Fxxk me! And submit a PR or something favorite

“Although I don’t think it’s likely anyone will pay attention to me.”