/*
 * 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 sendData = {
                type : 'message',
                message : 'test message!'
            };
            channel.sendToQueue(queueName, encode(sendData), {
                persistent: true
            });
            
        });
    });
});

function encode(doc) {  
//    return new Buffer(JSON.stringify(doc));
//	https://stackoverflow.com/questions/52165333/deprecationwarning-buffer-is-deprecated-due-to-security-and-usability-issues
//	new Buffer(string)            // Old
//	Buffer.from(string)           // New
	return Buffer.from(JSON.stringify(doc));
}