Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7947311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:23:43+00:00 2026-06-04T01:23:43+00:00

I have been working with RabbitMQ on .Net for a while already and I

  • 0

I have been working with RabbitMQ on .Net for a while already and I don’t have much of a problem with it. Now I’m moving to rabbit.js with node.js and I’m not pretty much familiar with it. rabbit.js has a limited documentation. All I know is the basic PUSH/PULL or PUB/SUB. Now I wanted to do REQ/REP and I don’t know how do it. Anybody can share some snippet please.

Your reply is greatly appreciated.

Best,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:23:45+00:00Added an answer on June 4, 2026 at 1:23 am

    This is perhaps more then you asked for but I have a snipplet (even though it’s quite long) for doing RPC using node-amqp instead of REQ/RES with rabbit.js. What I have done is similar to what you could find in the RabbitMQ tutorial about RPC

    For the moment the content in the message should be an object (hash) that will get transformed by the amqp module to json.

    The AmqpRpc class take an amqp connection when initialized then it should only be a matter of calling makeRequest and wait for a response in the callback.
    The response have the form of function(err, response) where err might be a timeout error

    I’m sorry its not exactly what you asked for but it’s maybe close enough.
    I also posted the code as a gist on github: https://gist.github.com/2720846

    Edit:
    Samples changed to support multiple outstanding requests.

    amqprpc.js

    var amqp = require('amqp')
      , crypto = require('crypto')
    
    var TIMEOUT=2000; //time to wait for response in ms
    var CONTENT_TYPE='application/json';
    
    exports = module.exports = AmqpRpc;
    
    function AmqpRpc(connection){
      var self = this;
      this.connection = typeof(connection) != 'undefined' ? connection : amqp.createConnection();
      this.requests = {}; //hash to store request in wait for response
      this.response_queue = false; //plaseholder for the future queue
    }
    
    AmqpRpc.prototype.makeRequest = function(queue_name, content, callback){
      var self = this;
      //generate a unique correlation id for this call
      var correlationId = crypto.randomBytes(16).toString('hex');
    
      //create a timeout for what should happen if we don't get a response
      var tId = setTimeout(function(corr_id){
        //if this ever gets called we didn't get a response in a 
        //timely fashion
        callback(new Error("timeout " + corr_id));
        //delete the entry from hash
        delete self.requests[corr_id];
      }, TIMEOUT, correlationId);
    
      //create a request entry to store in a hash
      var entry = {
        callback:callback,
        timeout: tId //the id for the timeout so we can clear it
      };
    
      //put the entry in the hash so we can match the response later
      self.requests[correlationId]=entry;
    
      //make sure we have a response queue
      self.setupResponseQueue(function(){
        //put the request on a queue
        self.connection.publish(queue_name, content, {
          correlationId:correlationId,
          contentType:CONTENT_TYPE,
          replyTo:self.response_queue});
      });
    }
    
    
    AmqpRpc.prototype.setupResponseQueue = function(next){
      //don't mess around if we have a queue
      if(this.response_queue) return next();
    
      var self = this;
      //create the queue
      self.connection.queue('', {exclusive:true}, function(q){  
        //store the name
        self.response_queue = q.name;
        //subscribe to messages
        q.subscribe(function(message, headers, deliveryInfo, m){
          //get the correlationId
          var correlationId = m.correlationId;
          //is it a response to a pending request
          if(correlationId in self.requests){
            //retreive the request entry
            var entry = self.requests[correlationId];
            //make sure we don't timeout by clearing it
            clearTimeout(entry.timeout);
            //delete the entry from hash
            delete self.requests[correlationId];
            //callback, no err
            entry.callback(null, message);
          }
        });
        return next();    
      });
    }
    

    A small example on how to use it can be found below. Save both code parts and just run with…

    node client.js
    

    If you don’t have a server to provide the reply the request will time out.

    client.js

    //exmaple on how to use amqprpc
    var amqp = require('amqp');
    var connection = amqp.createConnection({host:'127.0.0.1'});
    
    var rpc = new (require('./amqprpc'))(connection);
    
    connection.on("ready", function(){
      console.log("ready");
      var outstanding=0; //counter of outstanding requests
    
      //do a number of requests
      for(var i=1; i<=10 ;i+=1){
        //we are about to make a request, increase counter
        outstanding += 1;
        rpc.makeRequest('msg_queue', {foo:'bar', index:outstanding}, function response(err, response){
          if(err)
            console.error(err);
          else
            console.log("response", response);
          //reduce for each timeout or response
          outstanding-=1;
          isAllDone();
        });
      }
    
      function isAllDone() {
        //if no more outstanding then close connection
        if(outstanding === 0){
          connection.end();
        }
      }
    
    });
    

    I’ll even throw in a sample server for good measure

    server.js

    //super simple rpc server example
    var amqp = require('amqp')
      , util = require('util');
    
    var cnn = amqp.createConnection({host:'127.0.0.1'});
    
    cnn.on('ready', function(){
      console.log("listening on msg_queue");
      cnn.queue('msg_queue', function(q){
          q.subscribe(function(message, headers, deliveryInfo, m){
            util.log(util.format( deliveryInfo.routingKey, message));
            //return index sent
            cnn.publish(m.replyTo, {response:"OK", index:message.index}, {
                contentType:'application/json',
                contentEncoding:'utf-8',
                correlationId:m.correlationId
              });
          });
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with ASP.NET for a few years and am now working
I have been working on an application in Xcode for a while now and
I have been working with android for a little while now and feel pretty
I have been working on a project in C# (.net4). Project pretty much allows
I have been working with Zend for a few months now and am at
I have been working on a site for about a week now and we
I have been working on a system which I'm using protobuf-net (version 2.0.0.480) for
I have been working on a project for some time now, and I decided
I have been working on a count down for most of today, and now
I have been working on this for a while and am trying to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.