/*
 * npm install amqplib
 */

var amqp = require('amqplib/callback_api');

const url = 'amqp://dev-swh.ga:5672';
const queueName = 'hello';

amqp.connect(url, function(error, connect){
    if(error){
        console.log(error);
        return;
    }
    connect.createChannel(function(error, channel){
        if(error){
            console.log(error);
            return;
        }
        channel.assertQueue(queueName, {durable: true}, function(error){
            let recevieMessage = function(){
                channel.get(queueName, {}, function(error, message){
                    if(error){
                        console.log(error);
                    }
                    else if(message){
                        console.log(message.content.toString());
                        channel.ack(message);
//                        setTimeout(recevieMessage, 5000);
                    }
                    else{
//                        console.log('NO MESSAGE');
//                        setTimeout(recevieMessage, 5000);
                    }
                });
            }
            recevieMessage();
        });
    });
});