Created
October 15, 2016 01:42
-
-
Save marcpicaud/c9742e26991385ccd3d3d60410412950 to your computer and use it in GitHub Desktop.
This file contains 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 express = require('express'); | |
const bodyParser = require('body-parser'); | |
const amqp = require('amqplib'); | |
const q = 'tasks'; | |
var app = express(); | |
app.use(bodyParser.json()); | |
// Publisher | |
var open = amqp.connect('amqp://localhost'); | |
open.then((conn) => { | |
return conn.createChannel(); | |
}).then((ch) => { | |
return ch.assertQueue(q).then((ok) => { | |
app.post('/data', (req, res) => { | |
ch.sendToQueue(q, new Buffer(JSON.stringify(req.body))); | |
res.end(); | |
}); | |
app.listen('3000'); | |
}); | |
}).catch(console.warn); | |
// Consumer | |
open.then(function(conn) { | |
return conn.createChannel(); | |
}).then(function(ch) { | |
return ch.assertQueue(q).then(function(ok) { | |
return ch.consume(q, function(msg) { | |
if (msg !== null) { | |
console.log(msg.content.toString()); | |
ch.ack(msg); | |
} | |
}); | |
}); | |
}).catch(console.warn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment