Skip to content

Instantly share code, notes, and snippets.

@yunghoy
Last active June 2, 2022 00:34
Show Gist options
  • Select an option

  • Save yunghoy/a425f91824d26461bb2e3653bc56ebbf to your computer and use it in GitHub Desktop.

Select an option

Save yunghoy/a425f91824d26461bb2e3653bc56ebbf to your computer and use it in GitHub Desktop.

Revisions

  1. yunghoy revised this gist Sep 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ async function main() {
    const channel = await conn.createChannel();

    await channel.assertExchange(EXCHANGE_NAME, EXCHANGE_TYPE, EXCHANGE_OPTION);
    // ch.bindExchange('test', ex, '', {});
    // await channel.bindExchange('exchange_name_another_one', EXCHANGE_NAME, '', {});

    const q = await channel.assertQueue('', {exclusive: true});

  2. yunghoy revised this gist Sep 7, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    alias babel-node='babel-node --presets stage-0'


    ------ RECV ------
    // babel-node recv2.js "#"
    // babel-node recv2.js "kern.*"
  3. yunghoy revised this gist Sep 7, 2016. 1 changed file with 40 additions and 2 deletions.
    42 changes: 40 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ---
    ------ RECV ------
    // babel-node recv2.js "#"
    // babel-node recv2.js "kern.*"
    const amqp = require('amqplib');
    @@ -39,4 +39,42 @@ async function main() {


    main();
    ---
    ------ END OF RECV ------

    ------ SEND ------
    // babel-node send2.js "kern.critical" "A critical kernel error"

    const amqp = require('amqplib');

    const args = process.argv.slice(2);
    const key = (args.length > 0) ? args[0] : 'anonymous.info';
    const msg = args.slice(1).join(' ') || 'Hello World!';

    const EXCHANGE_NAME = 'exchange_name';
    const EXCHANGE_TYPE = 'x-recent-history';
    const EXCHANGE_OPTION = {
    durable: true,
    autoDelete: true,
    arguments: {
    'x-recent-history-length': 10
    }
    };

    async function main() {
    const conn = await amqp.connect('amqp://localhost');
    const channel = await conn.createChannel();

    await channel.assertExchange(EXCHANGE_NAME, EXCHANGE_TYPE, EXCHANGE_OPTION);
    channel.publish(EXCHANGE_NAME, key, new Buffer(msg));

    console.log(" [x] Sent %s:'%s'", key, msg);

    setTimeout(() => {
    conn.close();
    process.exit(0);
    }, 500);
    };


    main();
    ------ END OF SEND ------
  4. yunghoy created this gist Sep 7, 2016.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    ---
    // babel-node recv2.js "#"
    // babel-node recv2.js "kern.*"
    const amqp = require('amqplib');

    const args = process.argv.slice(2);
    if (args.length == 0) {
    console.log("Usage: receive_logs_topic.js <facility>.<severity>");
    process.exit(1);
    }

    const EXCHANGE_NAME = 'exchange_name';
    const EXCHANGE_TYPE = 'x-recent-history';
    const EXCHANGE_OPTION = {
    durable: true,
    autoDelete: true,
    arguments: {
    'x-recent-history-length': 10
    }
    };

    async function main() {
    const conn = await amqp.connect('amqp://localhost');
    const channel = await conn.createChannel();

    await channel.assertExchange(EXCHANGE_NAME, EXCHANGE_TYPE, EXCHANGE_OPTION);
    // ch.bindExchange('test', ex, '', {});

    const q = await channel.assertQueue('', {exclusive: true});

    console.log(' [*] Waiting for logs. To exit press CTRL+C');

    args.forEach((key) => channel.bindQueue(q.queue, EXCHANGE_NAME, key));

    channel.consume(q.queue, function(msg) {
    console.log(" [x] %s:'%s'", msg.fields.routingKey, msg.content.toString());
    }, {noAck: true});
    }


    main();
    ---