Same as account.getAccountId(). For compatible v1.x.
account.getKeyId()
Return the ali key id.
account.getGA() & account.setGA(bGA:boolean)
Gets or Sets the status of google analytics collection.
Set bGA to true for enabling google analytics, while set to false for disabling google analytics.
See Privacy Policy.
Gets or Sets using http or https protocol.
Set bHttps to true for using https, while set to false for using http protocol.
Default is false for using http protocol.
The Region class help your specifying the region of datacenter.
city: String | City, optional. It can be the data center name, like "hangzhou", "beijing" or "west-1", "southeast-2";
or it can be a pre-defined city enum value, like AliMNS.City.Beijing, AliMNS.City.Tokyo, check Region.ts#L169-L188 for the full list.
The default is "hangzhou".
network: String | NetworkType, optional.
If it is a string, should be ""(empty string) or "-internal" or "-internal-vpc".
If it is NetworkType enum, should be AliMNS.NetworkType.Public or AliMNS.NetworkType.Internal or AliMNS.NetworkType.VPC.
The default is ""(empty string), means AliMNS.NetworkType.Public network.
zone: String | Zone, optional.
If it is a string, should be data center zone, like "cn", "us", "eu", "me" or "ap".
If it is Zone enum, should be AliMNS.Zone.China, AliMNS.Zone.AsiaPacific, AliMNS.Zone.Europe, AliMNS.Zone.UniteState or AliMNS.Zone.MiddleEast.
The default is "cn", means AliMNS.Zone.China.
This value will be ignored if city parameter is a pre-defined city enum value, because we can deduce zone from city.
region: String|Region, optional.
If it is string, it can be "hangzhou", "beijing" or any Chinese datacenter city name.
If it is Region, it allows you to specify data center other than in China.
Default is "hangzhou". It can also be internal or vpc address "hangzhou-internal", "beijing-internal" or "qingdao-internal-vpc".
var AliMNS =require("ali-mns");
var account =newAliMNS.Account("<your-account-id>","<your-key-id>","<your-key-secret>");
var mns =newAliMNS.MNS(account,"hangzhou");
// or
var regionJapan =newAliMNS.Region(AliMNS.City.Japan,AliMNS.NetworkType.Public);
var mnsJapan =newAliMNS.MNS(account, regionJapan);
options.DelaySeconds: number. How many seconds will the messages be visible after sent. 0~604800(7days), default is 0.
options.MaximumMessageSize: number. How many bytes could the message be. 1024(1k)~65536, default is 65536(64k).
options.MessageRetentionPeriod: number. How many seconds will the messages live, 60~1296000(15days), default is 345600(4days).
optiions.VisibilityTimeout: number. How many seconds will the message keep invisible after be received, 1~43200(12hours), default is 30.
options.PollingWaitSeconds: numer. How many seconds will the receive request wait for if mq is empty. 0~30, default is 0.
mns.createP("myAliMQ",{
DelaySeconds:0,
MaximumMessageSize:65536,
MessageRetentionPeriod:345600,
VisibilityTimeout:30,
PollingWaitSeconds:0
}).then(console.log,console.error);
If a mq with same name exists, calling createP will succeed only when all of the mq attributes are all same.
Any mismatched attributes will cause an "QueueAlreadyExist" failure.
region: String|Region, optional.
If it is string, it can be "hangzhou", "beijing" or any Chinese datacenter city name.
If it is Region, it allows you to specify data center other than in China.
Default is "hangzhou". It can also be internal or vpc address "hangzhou-internal", "beijing-internal" or "qingdao-internal-vpc".
var AliMNS =require("ali-mns");
var account =newAliMNS.Account("<your-account-id>","<your-key-id>","<your-key-secret>");
var mq =newAliMNS.MQ(account,"hangzhou");
// or
var regionJapan =newAliMNS.Region(AliMNS.City.Japan,AliMNS.NetworkType.Public);
priority: number, optional. 1(lowest)~16(highest), default is 8.
delaySeconds: number, optional. How many seconds will the messages be visible after sent. 0~604800(7days), default is 0.
This argument is prior to the options.DelaySeconds in attributes of message queue.
Gets or sets the tolerance seconds for mq.recvP method.
value: number. Default is 5, in seconds. How long will mq.recvP wait before timeout.
Due to network lag, the return of mq.recvP method may be later than expected.
mq.recvP(waitSeconds?:number)
Receive a message from queue.
This will change the message to invisible for a while.
waitSeconds: number. optional.
The max seconds to wait if queue is empty, after that an error MessageNotExist will be returned.
mq.recvP(5).then(console.log,console.error);
This method will wait waitSeconds + getRecvTolerance() totally if queue is empty.
mq.peekP()
Peek a message.
This will not change the message to invisible.
mq.peekP(5).then(console.log,console.error);
mq.deleteP(receiptHandle:string)
Delete a message from queue.
A message will be invisible for a short time after received.
A message must be deleted after processed, otherwise it can be received again.
receiptHandle: String. Return by mq.recvP or mq.notifyRecv.
If you need more time to process the message after received, you can reserve it for a longer time.
The message will continue to keep invisible for reserveSeconds from now.
Set a shorter time is also possible.
If succeed, a new receiptHandle will be returned to replace the old one, further mq.deleteP or mq.reserveP should use the newer.
And the newer receiptHandle will expired after reserveSeconds past.
cb: The callback function will be called once for each received message.
And if the callback function return true, the message received will be delete automatically,
while you should delete the message manually, if return false.
waitSeconds: number, optional. 1~30. The max seconds to wait in a polling loop, default is 5.
At the begin of a polling loop, it will check if mq.notifyStopP has been called, So the bigger number
will cause a slowly mq.notifyStopP.
Set waitSeconds to 0 ,will actually use the default value 5 seconds instead.
mq.notifyRecv(function(err,message){
console.log(message);
if(err &&err.message==="NetworkBroken"){
// Best to restart the process when this occurs
throw err;
}
returntrue;// this will cause message to be deleted automatically
});
Both callback functions will work if you call notifyRecv twice for 2 different callback functions.
But each received message only will trigger one of them only.
mq.notifyStopP()
Stop mq.notifyRecv working. The promise object returned will not be resolved until the receiving loop stopped actually.
The max time wait for notifyRecv() stop is determined by waitSeconds passed to mq.notifyRecv.
mq.notifyStopP().then(console.log,console.error);
mq.getAttrsP()
Get the attributes of the mq.
mq.getAttrsP().then(console.log, console.error);
mq.setAttrsP(options:any)
Modify the attributes of mq.
options: the queue attributes. See the options of mns.createP.
priority: number, optional. 1(lowest)~16(highest), default is 8.
delaySeconds: number, optional. How many seconds will the messages be visible after sent. 0~604800(7days), default is 0.
This argument is prior to the options.DelaySeconds in attributes of message queue.
var msg =newAliMNS.Msg("Make a test");
msg.getMsg()
Return the content of message.
msg.getPriority()
Return the priority of message.
msg.getDelaySeconds()
Return the delay seconds of message.
MQBatch
Provide the batch process model introduced in a new edtion of Ali-MNS service in June, 2015.
It derives from MQ, so all methods in MQ are avaiable in MQBatch too. For example, you can
use mqBatch.setRecvTolerance(1.2) to adjust the timeout behavior of mqBatch.recvP().
var mqBatch =newAliMNS.MQBatch(aliCfg.mqName, account,aliCfg.region);
Send a message or batch send messages to the queue.
msg: String or an array of Msg. The message(s) up to 16 that sent to queue.
priority: number, optional. Only valid when msg is a string, 1(lowest)~16(highest), default is 8.
delaySeconds: number, optional. Only valid when msg is a string. How many seconds will the messages be visible after sent. 0~604800(7days), default is 0.
This argument is prior to the options.DelaySeconds in attributes of message queue.
If msg is an array of Msg, use the priority & delaySeconds properties of Msg, and ignore the 2nd and 3rd arguments.
Delete a message or messages from queue.
Messages will be invisible for a short time after received.
Messages must be deleted after processed, otherwise it can be received again.
receiptHandle: String or an array of string. Return by mq.recvP mq.notifyRecv or mqBatch.recvP mqBatch.notifyRecv.
region: String|Region, optional.
If it is string, it can be "hangzhou", "beijing" or any Chinese datacenter city name.
If it is Region, it allows you to specify data center other than in China.
Default is "hangzhou". It can also be internal or vpc address "hangzhou-internal", "beijing-internal" or "qingdao-internal-vpc".
var AliMNS =require("ali-mns");
var account =newAliMNS.Account("<your-account-id>","<your-key-id>","<your-key-secret>");
var topic =newAliMNS.Topic("t11", account,"shenzhen");
// or
var regionJapan =newAliMNS.Region(AliMNS.City.Japan,AliMNS.NetworkType.Public);
var topicJapan =newAliMNS.Topic("t11", account, regionJapan);
topic.getName()
Get topic name.
topic.getAccount()
Get topic account.
topic.getRegion()
Get topic region.
topic.getAttrsP() & topic.setAttrsP(options:any)
Get or set attributes of topic.
options: topic attributes.
options.MaximumMessageSize: int. The maximum size of message, 1024(1k)~65536(64k), default is 65536.
options.LoggingEnabled: boolean. Enable logging or not, default is false.
Set the environment variable DEBUG to "ali-mns" to enable the debug trace output.
# linux bash
export DEBUG=ali-mns
# windows
set DEBUG=ali-mns
Migrate
1.The ali-mns is fully compatible with ali-mqs, simply replace the ali-mqs package to ali-mns.
// var AliMQS = require('ali-mqs');
var AliMQS =require('ali-mns');
2.Optional. Change the ownerId to accountId
Ali-Yun upgrade their account system, and recommend to use the newer account id instead of owner id.
But the old owner id is still available for now.
var AliMQS =require("ali-mns");
// var account = new AliMNS.Account("hl35yqoedp", "<your-key-id>", "<your-key-secret>");
var account =newAliMNS.Account("1786090012649663","<your-key-id>","<your-key-secret>");
ownerId is mixed with number and letter
accountId is a 16-digits number,
follow this link to find your accountId.
In GitHub, An branch v1.x keeps tracking for the old mqs services.
And use `npm install ali-mqs' to install the ali-mqs package for v1.x.
Performance - Serial vs. Batch
Create 20 queues, then send 2000 messages to them randomly.
It is about 10 times slower in serial mode than in batch mode.
Set environment variable DEBUG to ali-mns.test to turn on output trace(will slow down the test).
Privacy Policy
We collect information about how you use the ali-mns packages for better service.
By default a tracing information is sent to google analytics when sending a request to ali-mns service,
The tracing information contains only the url.
Your data, key will not be sent.
Your account id is sent by hash to md5 value, so it can not be used tracking back to you.
You can check code about data collection.
You can always disable data collection as you wish.
var AliMNS =require("ali-mns");
var account =newAliMNS.Account("<your-account-id>","<your-key-id>","<your-key-secret>");
// Disable google analytics data collection
account.setGA(false);
var mq =newAliMNS.MQ("<your-mq-name>", account,"hangzhou");