var amqp = require('amqp');
var connection = amqp.createConnection();
// add this for better debuging
connection.on('error', function(e) {
console.log("Error from amqp: ", e);
});
// Wait for connection to become established.
connection.on('ready', function () {
// Use the default 'amq.topic' exchange
console.log("connected to----"+ connection.serverProperties.product);
connection.publish("my-queue",{hello:'world'});
});
var amqp = require('amqp');
var connection = amqp.createConnection();
// add this for better debuging
connection.on('error', function(e) {
console.log("Error from amqp: ", e);
});
// Wait for connection to become established.
connection.on('ready', function () {
// Use the default 'amq.topic' exchange
connection.queue('my-queue', function (q) {
// Catch all messages
q.bind('#');
// Receive messages
q.subscribe(function (message) {
// Print messages to stdout
console.log(message);
});
});
});
分别运行发布者和订阅者,可以看到订阅者接受到发布的消息。