Reputation: 2092
I'm using amqplib to publish messages to my RabbitMQ server. This is working quite well, but I think I can optimize the way I do it.
The idea is to listen to a serial port to get data from it (which is continuously sent) and publish this data to the AMQP channel. Then the subscribers get the data for two needs:
Here is my code:
function on_connect(err, conn) {
if (err !== null) return bail(err);
function on_channel_open(err, ch) {
if (err !== null) return bail(err, conn);
ch.assertQueue(amqp_channel, {durable: false}, function(err, ok) {
if (err !== null) return bail(err, conn);
var trameEvents = teleinfo('/dev/ttyAMA0');
trameEvents.on('tramedecodee', function (data) {
ch.sendToQueue(amqp_channel, new Buffer(data));
console.log("Sent '%s'", data);
});
//log errors
trameEvents.on('error', function (err) {
console.log(util.inspect(err));
});
ch.close(function() { conn.close(); });
});
}
conn.createChannel(on_channel_open);
}
The problem is the trameEvents returns sometimes an error, and then the whole application dies:
/home/pi/nodejs/teleinfo-amqp-publisher/node_modules/amqplib/lib/channel.js:149
throw new IllegalOperationError(msg, stack);
^
IllegalOperationError: Channel closed
at Channel.<anonymous> (/home/pi/nodejs/teleinfo-amqp-publisher/node_modules/amqplib/lib/channel.js:149:11)
So I'm looking for a way to make a robust node application doing these tasks. Any idea?
Thanks
Upvotes: 0
Views: 687
Reputation: 72858
ch.close(function() { conn.close(); });
you're telling the channel to close, which is causing the error you are seeing.
take that line of code out of your app and it should run indefinitely.
Upvotes: 1