공식 RabbitMQ 자바 스크립트 튜토리얼RabbitMQ의 amqp.node 통합
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var q = 'hello';
ch.assertQueue(q, {durable: false});
// Note: on Node 6 Buffer.from(msg) should be used
ch.sendToQueue(q, new Buffer('Hello World!'));
console.log(" [x] Sent 'Hello World!'");
});
});
그러나, 나는 그것이 다른 곳에서이 코드를 재사용하기 어렵다 찾을 amqp.node 클라이언트 라이브러리의 사용을 보여 표현한다. 특히, 채널 객체가 콜백되어 있기 때문에 채널 객체를 내보내는 방법을 알지 못합니다. 내 NodeJs/Express 앱의 예 :
app.post('/posts', (req, res) => {
-- Create a new Post
-- Publish a message saying that a new Post has been created
-- Another 'newsfeed' server consume that message and update the newsfeed table
// How do I reuse the channel 'ch' object from amqp.node here
});
이 지침에 대한 지침이 있습니까? 다른 라이브러리에 대한 제안을 환영합니다. (시작한 이래로, 가장 쉬운 것으로 생각되는 것이 사용 용이)
채널 개체를 내 보내야하는 경우 채널을 만드는 파일에서 'module.exports'를 사용할 수 있습니다. 예를 들어,'conn.createChannel' 콜백 안에서'module.exports.channel = ch'를 호출하십시오. 그런 다음 'channelCreatingScript.channel'과 같은 스크립트를 작성하는'필수 '파일을 다른 파일에서 액세스 할 수 있습니다. – ishmaelMakitla
시작됩니다 undefined – rocketspacer
수출 명세서를 외부에 넣을 수 있습니다 - 그리고 그것을 'null'또는 '{}'로 지정할 수 있습니다. 'module.exports.channel'을 통해 새로운 값을 할당하면 그것을 업데이트 할 것입니다. – ishmaelMakitla