title: Where to set up of the binding of exchange and queue (producer vs. consumer)?
body: <p>All of the <a href="https://www.rabbitmq.com/tutorials/tutorial-five-javascript.html" rel="noreferrer">official RabbitMQ examples</a> set up the queues and bindings in the <strong>consumer</strong>. The <a href="https://www.rabbitmq.com/tutorials/tutorial-three-javascript.html" rel="noreferrer">Publish/Subscribe tutorial</a> states that</p>
  
  <blockquote>
    <p>The messages will be lost if no queue is bound to the exchange yet, but that's 
    okay for us; if no consumer is listening yet we can safely discard the message.</p>
  </blockquote>
  
  <p>This is absolutely not okay for me, because I'm implementing a job worker queue on top of RabbitMQ, and it is important to not lose any messages when the consumer hasn't run yet. Therefore, I'm thinking of establishing the exchange &lt;-> queue routing in the <strong>producer</strong>. Is there a reason why the examples do it the other way around?</p>
  
  <p>As an aside, is it considered best practice to do the basic exchange/queue/routing setup every time I connect to the RabbitMQ server or just once (ever) to basically configure the RabbitMQ instance? My current approach to publish a message currently looks a bit like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>const getChannel = () =&gt;    
    ampq.connect() // The real implementation caches the connection
      .then(conn =&gt; conn.createChannel())
      .then(channel =&gt; channel.assertExchange(...)
        .then(() =&gt; channel.assertQueue(...)) // Assert and bind for all queues
        .then(() =&gt; channel.bindQueue(...))   // Assert and bind for all queues
      );
  
  const publish = (task, payload) =&gt; 
    getChannel().then(channel =&gt; 
      channel.publish(exchange, task, payload)
    );
  </code></pre>