Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

Here you can find consumers examples using different programming languages.

The official RabbitMQ site contains a very useful tutorial with code examples. But if you want to run your consuming application right now, you can use examples on this page

Connection parameters

All of these examples use the following connection parameters:

  • Protocol:

    • AMQP - if you don’t use SSL connection

    • AMQPS - if you use SSL connection. This protocol is used by default on AWS managed RabbitMQ.

  • Hostname

  • Username

  • Password

  • Port (usually it is 5671 or 5672)

  • Virtual hostname (vHost)

  • Exchange name

  • Queue name

All our examples use exchange type “direct” and “durable” exchanges and queues.

Examples

JavaScript

Java script example code uses package amqplib. Please, install the package before proceeding with an example:

npm install amqplib

Now you are able to fill in your connection parameters into the script and start consuming:

#!/usr/bin/env node

var amqp = require('amqplib/callback_api');

var broker_url = 'amqps://<username>:<password>@<host>:<port>/<vhost>'
var exchange = '<exchange_name>'
var queue = '<queue_name>'


amqp.connect(broker_url, function(error0, connection) {
  if (error0) {
    throw error0;
  }
  connection.createChannel(function(error1, channel) {
    if (error1) {
      throw error1;
    }

    channel.assertExchange(exchange, 'direct', {
      durable: true
    });

    channel.assertQueue(queue, {
      durable: true
      }, function(error2, q) {
        if (error2) {
          throw error2;
        }

      channel.bindQueue(q.queue, exchange, queue);

      console.log(' [*] Waiting for data. To exit press CTRL+C');
      channel.consume(q.queue, function(msg) {
        console.log(" [x] %s: '%s'", msg.fields.routingKey, msg.content.toString());
      }, {
        noAck: true
      });
    });
  });
});

Python

Python example uses package pika:

pip install pika

This example code is able to run with using Python 3.6 or higher. You can remove f-string using and run the code on earlier Python3 version.

#!/usr/bin/env python3.8

import pika
import sys
import os


RMQ_QUEUE = '<queue_name>'
RMQ_HOST = '<host>'
RMQ_PORT = <port>
RMQ_VHOST = '<vhost>'
RMQ_LOGIN = '<username>'
RMQ_PASS = '<password>''
RMQ_EXCHANGE = '<exchange_name>'


URL = f'amqp://{RMQ_LOGIN}:{RMQ_PASS}@{RMQ_HOST}:{RMQ_PORT}/{RMQ_VHOST}'

def main():

    parameters = pika.URLParameters(URL)
    rmq_connection = pika.BlockingConnection(parameters)
    rmq_channel = rmq_connection.channel()
    rmq_channel.exchange_declare(
        exchange=RMQ_EXCHANGE,
        exchange_type='direct',
        durable=True
    )
    rmq_channel.queue_declare(
        queue=RMQ_QUEUE,
        durable=True
    )

    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)

    rmq_channel.basic_consume(
        queue=RMQ_QUEUE,
        on_message_callback=callback,
        auto_ack=True
    )

    print(' [*] Waiting for messages. To exit press CTRL+C')
    rmq_channel.start_consuming()


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)

  • No labels