Skip to content

Instantly share code, notes, and snippets.

@Livshitz
Last active March 29, 2021 15:39
Show Gist options
  • Save Livshitz/4114949d7d73bb30f02a393f76bc9a64 to your computer and use it in GitHub Desktop.
Save Livshitz/4114949d7d73bb30f02a393f76bc9a64 to your computer and use it in GitHub Desktop.
Quick & easy way to push messages to RMQ through HTTP API.
// rabbitMQ_pusher:
// Quick & easy way to push messages to RMQ through HTTP API.
// Elya Livshitz 2020
// PLEASE NOTE: the destination queue is defined in each message, not here in the url...
/*
Couple notes:
1. Grab the messages and save to a local json file from CloudAMQP by opening devtools>network and ‘get’ the messages with nack [img 1](https://user-images.githubusercontent.com/246724/112861814-d1eb2780-90bd-11eb-96e9-b9bc2cb5bc2d.png)
2. Grab `amqpToken` from Authorization [img 2](https://user-images.githubusercontent.com/246724/112861830-d6174500-90bd-11eb-88bf-8443408d701f.png)
3. Grab `url` from ‘publish’ [img 3](https://user-images.githubusercontent.com/246724/112861839-d9123580-90bd-11eb-8a77-d023b4138d16.png).
4. Edit `messages.json` file and replace the queue name in `routing_key` field to the destination queue you want
*/
const fs = require('fs');
const _fetch = require('node-fetch');
const { Deferred } = require('concurrency.libx.js');
const sourceFile = __dirname + '/messages.json';
const amqpToken = '';
const url = '';
async function send(item) {
// PLEASE NOTE: the destination queue is defined in each message, not here in the url...
const promise = new Deferred();
_fetch(url, {
"credentials": "include", "headers": { "accept": "*/*", "authorization": "Basic " + amqpToken, "content-type": "text/plain;charset=UTF-8" },
"body": JSON.stringify(item), "method": "POST", "mode": "cors"
}).then(function (response) {
const res = {
date: response.headers.get('Date'),
status: response.status,
response: response.statusText,
}
promise.resolve(res);
}).catch(promise.reject);
return promise;
}
const messagesStr = fs.readFileSync(sourceFile);
const messages = JSON.parse(messagesStr);
const run = async ()=> {
for(let msg of messages) {
// console.log('msg: ', msg);
const res = await send(msg);
if (res.status !== 200) throw new Error(res.response);
console.log('res: ', res);
}
};
run().then(()=>console.log('DONE!'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment