Created
February 8, 2019 03:47
-
-
Save lenconda/d2e028fa4094fcad51b3ab4e778d32e1 to your computer and use it in GitHub Desktop.
Basic Producer-Consumer model with Node.js and RabbitMQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const amqp = require('amqp') | |
const conn = amqp.createConnection({ | |
host: '127.0.0.1', | |
port: 5672, | |
login: 'guest', | |
password: 'guest', | |
connectionTimeout: 10000 | |
}) | |
conn.on('ready', () => { | |
// 和 publisher 使用的队列名称一致 | |
conn.queue('test', queue => { | |
queue.bind('#') | |
queue.subscribe(message => { | |
let payload = unescape(message.data) | |
console.log(`CONSUME ${payload}`) | |
}) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const amqp = require('amqp') | |
const conn = amqp.createConnection({ | |
host: '127.0.0.1', | |
port: 5672, | |
login: 'guest', | |
password: 'guest', | |
connectionTimeout: 10000 | |
}) | |
conn.on('ready', () => { | |
let count = 1 | |
setInterval(() => { | |
let message = `TEST ${count}` | |
// 如果 test 不存在,就新建一个名为 test 的队列 | |
conn.publish('test', message) | |
console.log(`SENT ${count} - ${message}`) | |
count += 1 | |
}, 1000) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment