Skip to content

Instantly share code, notes, and snippets.

@elderbas
Created November 17, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elderbas/60d7c920a69e145aa922080ef9922759 to your computer and use it in GitHub Desktop.
Save elderbas/60d7c920a69e145aa922080ef9922759 to your computer and use it in GitHub Desktop.
rabbit mq location create and modify example snippets
#!/usr/bin/env node
const amqp = require('amqplib/callback_api');
const RABBIT_MQ_EXCHANGE_TYPES = {
FANOUT: 'fanout',
};
function consumePublishedCreationLocation(exchangeName, consumeCb) {
amqp.connect('amqp://localhost', function(error0, connection) {
if (error0) {
throw error0;
}
// virtual connection
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
channel.assertExchange(exchangeName, RABBIT_MQ_EXCHANGE_TYPES.FANOUT, {
durable: false
});
// empty string means for rabbitMQ to use a random queue name
channel.assertQueue('', {
exclusive: true
}, function(error2, q) {
if (error2) {
throw error2;
}
console.log("' [*] Waiting for messages in queue: %s, exchange: %s. To exit press CTRL+C", q.queue, exchangeName);
// for the QUEUE we just asserted,
// let's bind it to this exchange (queue -> exchange name)
channel.bindQueue(q.queue, exchangeName, '');
// now we subscribe our process as a consumer to this queue
channel.consume(q.queue, function(msg) {
if(msg.content) {
console.log(" [x] %s", );
cb(msg.content.toString());
}
}, {
noAck: true
});
});
});
});
}
consumePublishedCreationLocation('location.create', function (content) {
// use your JSON string Location
});
consumePublishedCreationLocation('location.modify', function (content) {
// use your JSON string Location
});
// physical connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment