title: RabbitMQ RPC using Node.js
body: <p>I use RabbitMQ to implement RPC in Node.js.</p>
  
  <p>I follow the tutorial, I assert the same name of queue for every client as 'rpc_client' in order to be high-authority,here is the <code>client.js</code> to call the function in the server:</p>
  
  <pre><code>const amqp = require('amqplib');
  
  async function client(){
      let args = process.argv.slice(2);
      let corr = generateUuid();
      let num = parseInt(args[0]);
      try {
          let conn = await amqp.connect('amqp://127.0.0.1');
          let ch = await conn.createChannel();
          let q = await ch.assertQueue('rpc_client');
  
          console.log(' [x] Requesting fib(%d)', num);
          console.log(q.queue)
  
          await ch.consume(q.queue,msg=&gt;{
              if (msg.properties.correlationId == corr) {
                  console.log(' [.] Got %s', msg.content.toString());
                  ch.ack(msg)
                  setTimeout(function() { conn.close(); process.exit(0) }, 5500);
              }
          },{noAck:false});
  
          ch.sendToQueue('rpc_server',
          new Buffer(num.toString()),
          { correlationId: corr, replyTo: q.queue });
  
      } catch(err){
          console.error(err);
      }
  }
  
  function generateUuid() {
      return Math.random().toString() +
             Math.random().toString() +
             Math.random().toString();
  }
  
  client();
  </code></pre>
  
  <p>but I found when I run several clients at a time, the latter client will not run the consume callback (get answer from the server and print it) until the former client's connection has been closed. e.g. The second client will get the answer and print it and its connection will closed in 5500 ms, and the second one has to wait the first one to close and it will print the answer then wait another 5500 ms to close.</p>
  
  <p>So why shoud this be? since the queue can consume the two massages in two worker parallelly.</p>
  
  <p>and here is the <code>server.js</code>:</p>
  
  <pre><code>async function server(){
      try {
          let conn = await amqp.connect('amqp://127.0.0.1');
          let ch = await conn.createChannel();
          process.once('SIGINT',()=&gt;conn.close());
  
          let q = await ch.assertQueue('rpc_server');
          ch.prefetch(1);
          console.log(' [x] Awaiting RPC requests');
  
          await ch.consume(q.queue,msg=&gt;{
              let n = parseInt(msg.content.toString());
  
              console.log(" [.] fib(%d)", n);
  
              let r = fibonacci(n);
  
              ch.sendToQueue(msg.properties.replyTo,
              new Buffer(r.toString()),
              {correlationId: msg.properties.correlationId});
  
              ch.ack(msg);
          },{noAck:false});
  
  
      } catch(err) {
          console.error(err);
      }
  }
  
  server();
  
  function fibonacci (n , ac1 = 1 , ac2 = 1) {
      if( n &lt;= 1 ) {return ac2};
  
      return fibonacci (n - 1, ac2, ac1 + ac2);
  }
  </code></pre>