title: How to manage publish connection per request on rabbitmq(rascal.js)
body: <p>I am using Rascal.Js(it uses amqplib) for my messaging logic with rabbitMq on node.js app.</p>
  <p>I am using something similar to their example on my project startup, which creates a permanent instance and &quot;registers&quot; all of my subscribers and redirects messages when they arrive to the queue (in the background).</p>
  <p>My issue is with the publishers. There are http requests from outside which should trigger my publishers. A user clicks on create button of sorts which leads to certain flow of actions. At some point it reaches the point at which I need to use a publisher.</p>
  <p>And here I am not sure about the right approach. Do I need to open a new connection every time I need to publish a message? and close it after it ends? Or maybe I should implement this in a way that it keeps the same connection open for all of the publishers? (I actually not so sure how to create it in a way that it can be accessed from other parts of my app).</p>
  <p>At the moment I am using the following :</p>
  <pre><code>async publishMessage(publisherName, message) {
          const dynamicSettings = setupDynamicVariablesFromConfigFiles(minimalPublishSettings);
          const broker = await Rascal.BrokerAsPromised.create(Rascal.withDefaultConfig(dynamicSettings.rascal));
  
          broker.on('error', async function(err) {
              loggerUtil.writeToLog('error', 'publishMessage() broker_error_event: ' + publisherName + err + err.stack);
              await broker.shutdown();
          })
     
          const publication = await broker.publish(publisherName, message);
          try {
              publication.on('error', async function(err) {
                  loggerUtil.writeToLog('error', 'publishMessage() publish_error_event: ' + err + err.stack);
                  await broker.shutdown();
              }).on(&quot;success&quot;, async (messageId) =&gt; {
                  await broker.shutdown();
              }).on(&quot;return&quot;, async (message) =&gt; {
                  loggerUtil.writeToLog('error', 'publishMessage() publish_return_event: ' + err + err.stack);
                  await broker.shutdown();
              })
          }
          catch(err) {
              loggerUtil.writeToLog('error', 'Something went wrong ' + err + err.stack);
              await broker.shutdown();
          }
  
      }
  </code></pre>
  <p>I use this function from different parts of my app when I need to publish messages.
  I thought to just add the broker.shutdown() for all of the endpoints but at some point after an error, I got an exception about closing a connection which was already closed, and this got me worried about the shutdown approach (which probably not a good one). I think it is related to this -
  I tried doing that (the commented code) but I think it isnt working well in certain situations.  If everything is ok it goes to &quot;success&quot; and then I can close it.
  But one time I had an error instead of success and when I tried to use broker.shutdown() it gave me another exception which crashed the app.  I think it is related to this -
  <a href="https://github.com/squaremo/amqp.node/issues/111" rel="nofollow noreferrer">https://github.com/squaremo/amqp.node/issues/111</a></p>
  <p>I am not sure what might be the safest way to approach this?</p>
  <p>Edit:</p>
  <p>Actually now that I think about it, the exception might be related to me trying to shutdown the broker in the catch{} area as well. I will continue to investigate.</p>