Search code examples
node.jsrabbitmqcrudmessaging

How can i usw crud operations with rabbitmq and nodejs?


I m new to this topic but still want to Know how to approach to this. I want to build a system that uses messaging to perform crud operations on a nodejs Server. I Know about Rest but i cant figure out how to translate it to messaging with rabbitmq.

edit: I think i have to make my questin a bit more clear: What i want to do is sending a message produced by my Java client using amqp and rabbitMQ to a node.js server. The message contains a JSON object. Some of the data should be send into the database(mysql).

My code looks some kind like this(Java Producer):

JSONObject obj = new JSONObject();      
          obj.put("fuellstand", behaelter_1.getFuellstand());
          obj.put("behaelter", behaelter_1.getId());
          obj.put("timestamp", currentTimestamp);
    //String message = behaelter_1.getFuellstand()+" "+ behaelter_1.getId()+" "+currentTimestamp;
          String message = obj.toJSONString();
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    //channel.basicPublish("",QUEUE_NAME , , arg3);
    System.out.println(message+" "+message.getBytes("UTF-8"));

And thats how my nodejs server trys to consume it:

amqp.connect('amqp://localhost', function (err, conn) {
if (err) {
    console.log("fehler mit dem amqp host!")
    throw(err);
} else {
    conn.createChannel(function (err, ch) {
        if (err) {
            console.log("failing to createChanel")
            throw(err);
        } else {
            var q = 'alerts';
            ch.assertQueue(q, {durable: false});
            console.log(" [*] Waiting for something in %s. CTRL+C to end", q);
            ch.consume(q, function (msg) {
                console.log(msg);
            }, {noAck: true});
        }
    });
}

});

the console returns the following:

{ fields: { consumerTag: 'amq.ctag-G3vsZRIGRZJT1qntZ1hTuw',
 deliveryTag: 1,
 redelivered: false,
 exchange: '',
 routingKey: 'alerts' },properties: {},content: <Buffer 7b 22 66 75 65 6c 6c 73 74 61 6e 64 22 3a 32 32 2c 22 62 65 68 61 65 6c 74 65 72 22 3a 31 2c 22 74 69 6d 65 73 74 61 6d 70 22 3a 32 30 31 36 2d 31 32 ... > }

my only problem at this point is to decode the json j build. I dont get why i cant decode the buffer. or am i getting something wrong?


Solution

  • RabbitMQ is not a database and does not support CRUD operations