Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

node-kafka中获取消息

$
0
0

最近在写node-kafka的应用,但是遇到一个问题,就是读取消息的时候,node读取kafka的消息还没读完,如果不延时获取信息的话,是拿不到数据的,请问该如何操作? 以下为我写的代码:

exports.ognvPullMsg			= function (req, res) {
var userId				= req.body.userId;
if (userId == 'undefined' || userId <= 0) {
	res.send('{"state":"301","message":"\u975e\u6cd5userId"}');
}
var Consumer			= kafka.Consumer;
var client				= new kafka.Client();
var consumer			= new Consumer(client,[{ topic: 'ognv-backend-log-'+userId, partition: 0 }],{autoCommit: false});
var realData			= [];
consumer.on('message', function (message) {
	console.log(message);
	realData.push(message);
});

setTimeout(function() {
	res.send(JSON.stringify(realData));
}, 200);

}


Viewing all articles
Browse latest Browse all 14821

Trending Articles