Skip to content

Instantly share code, notes, and snippets.

@lenconda
Created February 8, 2019 03:47
Show Gist options
  • Save lenconda/d2e028fa4094fcad51b3ab4e778d32e1 to your computer and use it in GitHub Desktop.
Save lenconda/d2e028fa4094fcad51b3ab4e778d32e1 to your computer and use it in GitHub Desktop.
Basic Producer-Consumer model with Node.js and RabbitMQ
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}`)
})
})
})
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