Redis MQ
Posted at 2016-10-16 Redis MQ Koa2
範例原始碼
安裝
12345678
npm init -ynpm install --save-dev babel-clinpm install --save-dev babel-preset-es2015npm install --save-dev babel-preset-stage-0npm install --save babel-polyfillnpm install --save-dev koa@2npm install --save rsmqnpm install --save rsmq-worker
送訊息到MQ
123456789101112131415161718
import RedisSMQ from "rsmq";const rsmq = new RedisSMQ({ host: process.env.REDIS_HOST || '127.0.0.1', port: process.env.REDIS_PORT || 6379});function send(qname, message) { rsmq.sendMessage({ qname: qname, message: message }, function(error, resp) { if (error) return console.error(error); if (resp) return console.log("ID:", resp); });}send(channelName, '098765 test');
監聽事件
123456789101112131415161718192021
import RSMQWorker from "rsmq-worker";var channelName = 'myqueue';var worker = new RSMQWorker(channelName);worker.on("message", function(msg, next, id) { console.log("Message id : " + id); console.log(msg); next()});worker.on('error', function(err, msg) { console.log("ERROR", err, msg.id);});worker.on('exceeded', function(msg) { console.log("EXCEEDED", msg.id);});worker.on('timeout', function(msg) { console.log("TIMEOUT", msg.id, msg.rc);});worker.start();
參考
Previous post: 初探 ThinkJS 2.x (1) Next post: Date 轉換 moment 格式