├── README.md ├── .gitattributes ├── package.json ├── LICENSE └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # node-queue 2 | A simple queue implementation in Node.js 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-queue", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Swathi Prasad", 10 | "license": "MIT", 11 | "dependencies": { 12 | "amqplib": "^0.5.5" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Swathi Prasad 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const amqp = require('amqplib/callback_api'); 2 | 3 | amqp.connect('amqp://localhost', function(error, connection) { 4 | if (error) { 5 | throw error; 6 | } 7 | connection.createChannel(function(error1, channel) { 8 | if (error1) { 9 | throw error1; 10 | } 11 | 12 | let queue = 'node_queue'; 13 | let msg = 'Test message'; 14 | 15 | channel.assertQueue(queue, { 16 | durable: true 17 | }); 18 | channel.sendToQueue(queue, Buffer.from(msg), { 19 | persistent: true 20 | }); 21 | console.log("Sent '%s'", msg); 22 | }); 23 | setTimeout(function() { 24 | connection.close(); 25 | process.exit(0) 26 | }, 500); 27 | }); 28 | 29 | 30 | amqp.connect('amqp://localhost', function(error0, connection) { 31 | if (error0) { 32 | throw error0; 33 | } 34 | connection.createChannel(function(error1, channel) { 35 | if (error1) { 36 | throw error1; 37 | } 38 | var queue = 'node_queue'; 39 | 40 | channel.assertQueue(queue, { 41 | durable: true 42 | }); 43 | channel.prefetch(1); 44 | 45 | console.log("Waiting for messages in %s", queue); 46 | channel.consume(queue, function(msg) { 47 | 48 | console.log("Received '%s'", msg.content.toString()); 49 | 50 | setTimeout(function() { 51 | channel.ack(msg); 52 | }, 1000); 53 | }); 54 | }); 55 | }); --------------------------------------------------------------------------------