Advertisement
vitareinforce

publisher

Oct 16th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. var amqp = require('amqplib/callback_api');
  2. var readline = require('readline');
  3.  
  4. amqp.connect('amqp://TMDG2022:TMDG2022@rmq2.pptik.id:5672/', function(error0, connection) {
  5. if (error0) {
  6. throw error0;
  7. }
  8. connection.createChannel(function(error1, channel) {
  9. if (error1) {
  10. throw error1;
  11. }
  12. var queue = 'testing';
  13.  
  14. // Create a readline interface to read from the keyboard
  15. var rl = readline.createInterface({
  16. input: process.stdin,
  17. output: process.stdout
  18. });
  19.  
  20. // Function to send a message to the queue
  21. function sendMessage() {
  22. rl.question('Enter a message to send: ', function(msg) {
  23. channel.assertQueue(queue, {
  24. durable: false
  25. });
  26.  
  27. channel.sendToQueue(queue, Buffer.from(msg));
  28. console.log(" [x] Sent %s", msg);
  29.  
  30. // Ask for another message or close the connection
  31. rl.question('Do you want to send another message? (yes/no): ', function(answer) {
  32. if (answer.toLowerCase() === 'yes') {
  33. sendMessage();
  34. } else {
  35. connection.close();
  36. process.exit(0);
  37. }
  38. });
  39. });
  40. }
  41.  
  42. sendMessage(); // Start the process by sending the first message
  43. });
  44. });
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement