How do I split up my rabbitMQ code across components?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?How to check whether a string contains a substring in JavaScript?How do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?

HackerRank Implement Queue using two stacks Solution

What exactly is Rhumb-line control in the context of a launch trajectory?

Gödel's paradox: Why is "a proof that some universal statement is unprovable" not a valid proof that this statement is true?

What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?

Have you been refused entry into the Federal Republic of Germany?

Does the problem of P vs NP come under the category of Operational Research?

Is an "are" omitted in this sentence

Is law enforcement responsible for damages made by a search warrant?

Is Norway in the Single Market?

Why did the United States not resort to nuclear weapons in Vietnam?

Is there a general term for the items in a directory?

Subtle ways to render a planet uninhabitable

Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?

Empty proof as standalone

Does WSL2 runs Linux in a virtual machine or alongside windows Kernel?

A verb for when some rights are not violated?

In-Cabinet (sink base) electrical box - Metal or Plastic?

Programming table cell colour based on contents

A wiild aanimal, a cardinal direction, or a place by the water

how to change ^L code in many files in ubuntu?

How long should I wait to plug in my refrigerator after unplugging it?

Representation of the concatenation at the type level

How do I find version of Intel graphics card drivers installed?

Counterexample with finite index



How do I split up my rabbitMQ code across components?


How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?How to check whether a string contains a substring in JavaScript?How do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to split my rabbitMQ connection code and call it across different components, so that it (the connection and channel) only initializes ONCE and I can use it whenever instead of having to open the connection again when I want to use it.



What happens right now is, I call the below's code function over and over again everytime I want to pass something to my exchange and queue. (so if I want to pass 20 individual data to rabbitMQ, I ended up opening and closing both the connection and channel 20 times)



Any solutions?



const exchange = "Exchange";
const queue = "Queue";

const passSomeData= async payload =>
amqp = require("amqplib").connect("amqp://localhost");
let ch;
let connection;
let publish = amqp
.then(function(conn)
connection = conn;
return conn.createConfirmChannel();
)
.then(function(chn)
ch = chn;
ch.assertQueue(queue, durable: true );
return ch.assertExchange(exchange, "topic", durable: true );
)
.then(function()
const data =
content: "x",
title: "y",
;
ch.bindQueue(queue, exchange, "routingKey");

return ch.publish(exchange, "routingKey", Buffer.from(JSON.stringify(data)),
persistent: true
);
)
.then(() =>
setTimeout(function()
connection.close();
, 250);
);
;

module.exports = passSomeData;









share|improve this question
































    0















    I want to split my rabbitMQ connection code and call it across different components, so that it (the connection and channel) only initializes ONCE and I can use it whenever instead of having to open the connection again when I want to use it.



    What happens right now is, I call the below's code function over and over again everytime I want to pass something to my exchange and queue. (so if I want to pass 20 individual data to rabbitMQ, I ended up opening and closing both the connection and channel 20 times)



    Any solutions?



    const exchange = "Exchange";
    const queue = "Queue";

    const passSomeData= async payload =>
    amqp = require("amqplib").connect("amqp://localhost");
    let ch;
    let connection;
    let publish = amqp
    .then(function(conn)
    connection = conn;
    return conn.createConfirmChannel();
    )
    .then(function(chn)
    ch = chn;
    ch.assertQueue(queue, durable: true );
    return ch.assertExchange(exchange, "topic", durable: true );
    )
    .then(function()
    const data =
    content: "x",
    title: "y",
    ;
    ch.bindQueue(queue, exchange, "routingKey");

    return ch.publish(exchange, "routingKey", Buffer.from(JSON.stringify(data)),
    persistent: true
    );
    )
    .then(() =>
    setTimeout(function()
    connection.close();
    , 250);
    );
    ;

    module.exports = passSomeData;









    share|improve this question




























      0












      0








      0








      I want to split my rabbitMQ connection code and call it across different components, so that it (the connection and channel) only initializes ONCE and I can use it whenever instead of having to open the connection again when I want to use it.



      What happens right now is, I call the below's code function over and over again everytime I want to pass something to my exchange and queue. (so if I want to pass 20 individual data to rabbitMQ, I ended up opening and closing both the connection and channel 20 times)



      Any solutions?



      const exchange = "Exchange";
      const queue = "Queue";

      const passSomeData= async payload =>
      amqp = require("amqplib").connect("amqp://localhost");
      let ch;
      let connection;
      let publish = amqp
      .then(function(conn)
      connection = conn;
      return conn.createConfirmChannel();
      )
      .then(function(chn)
      ch = chn;
      ch.assertQueue(queue, durable: true );
      return ch.assertExchange(exchange, "topic", durable: true );
      )
      .then(function()
      const data =
      content: "x",
      title: "y",
      ;
      ch.bindQueue(queue, exchange, "routingKey");

      return ch.publish(exchange, "routingKey", Buffer.from(JSON.stringify(data)),
      persistent: true
      );
      )
      .then(() =>
      setTimeout(function()
      connection.close();
      , 250);
      );
      ;

      module.exports = passSomeData;









      share|improve this question
















      I want to split my rabbitMQ connection code and call it across different components, so that it (the connection and channel) only initializes ONCE and I can use it whenever instead of having to open the connection again when I want to use it.



      What happens right now is, I call the below's code function over and over again everytime I want to pass something to my exchange and queue. (so if I want to pass 20 individual data to rabbitMQ, I ended up opening and closing both the connection and channel 20 times)



      Any solutions?



      const exchange = "Exchange";
      const queue = "Queue";

      const passSomeData= async payload =>
      amqp = require("amqplib").connect("amqp://localhost");
      let ch;
      let connection;
      let publish = amqp
      .then(function(conn)
      connection = conn;
      return conn.createConfirmChannel();
      )
      .then(function(chn)
      ch = chn;
      ch.assertQueue(queue, durable: true );
      return ch.assertExchange(exchange, "topic", durable: true );
      )
      .then(function()
      const data =
      content: "x",
      title: "y",
      ;
      ch.bindQueue(queue, exchange, "routingKey");

      return ch.publish(exchange, "routingKey", Buffer.from(JSON.stringify(data)),
      persistent: true
      );
      )
      .then(() =>
      setTimeout(function()
      connection.close();
      , 250);
      );
      ;

      module.exports = passSomeData;






      javascript node.js express rabbitmq backend






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 2:01







      Louis Leonid

















      asked Mar 27 at 1:39









      Louis LeonidLouis Leonid

      113 bronze badges




      113 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0














          Answer copied from here



          This is a general Javascript question and not one specific to RabbitMQ or the amqplib library.



          I believe you can open a connection at the module level and use that within your passSomeData method. Or, passSomeData can lazily open a connection if the module-level "connection" variable is null, and then re-use that connection.



          At some point you may need to use a connection pool, but that depends on your use-case and workload.




          NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368556%2fhow-do-i-split-up-my-rabbitmq-code-across-components%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Answer copied from here



            This is a general Javascript question and not one specific to RabbitMQ or the amqplib library.



            I believe you can open a connection at the module level and use that within your passSomeData method. Or, passSomeData can lazily open a connection if the module-level "connection" variable is null, and then re-use that connection.



            At some point you may need to use a connection pool, but that depends on your use-case and workload.




            NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






            share|improve this answer





























              0














              Answer copied from here



              This is a general Javascript question and not one specific to RabbitMQ or the amqplib library.



              I believe you can open a connection at the module level and use that within your passSomeData method. Or, passSomeData can lazily open a connection if the module-level "connection" variable is null, and then re-use that connection.



              At some point you may need to use a connection pool, but that depends on your use-case and workload.




              NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






              share|improve this answer



























                0












                0








                0







                Answer copied from here



                This is a general Javascript question and not one specific to RabbitMQ or the amqplib library.



                I believe you can open a connection at the module level and use that within your passSomeData method. Or, passSomeData can lazily open a connection if the module-level "connection" variable is null, and then re-use that connection.



                At some point you may need to use a connection pool, but that depends on your use-case and workload.




                NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






                share|improve this answer













                Answer copied from here



                This is a general Javascript question and not one specific to RabbitMQ or the amqplib library.



                I believe you can open a connection at the module level and use that within your passSomeData method. Or, passSomeData can lazily open a connection if the module-level "connection" variable is null, and then re-use that connection.



                At some point you may need to use a connection pool, but that depends on your use-case and workload.




                NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 15:47









                Luke BakkenLuke Bakken

                4,4702 gold badges11 silver badges20 bronze badges




                4,4702 gold badges11 silver badges20 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368556%2fhow-do-i-split-up-my-rabbitmq-code-across-components%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript