1.0.2 • Published 4 years ago

@notants/amqp-adapter v1.0.2

Weekly downloads
3
License
ISC
Repository
-
Last release
4 years ago

RabbitMQ adapter to publish/suscribe


How it works

The current adapter has two public methods:

1. Publish a message to a queue.

This method receive two parameters: queue_name and item. The queue_name is the queue where the message will be sent. The item is an object with the information to be passed. This information can be a log message inside an object or a more complex object (like sensor data).

2. Subscribe to a queue

This method will be consumed by the consumer. It keeps waiting indefinitely until the service's connection is broken or closed. This method receives two parameters: queue_name where the consumer will be listening or subscribed, and callback which is the function to be executed when a new message is received in the queue. This method allow us to have different consumers with different callbacks for the same queue.


How to execute it

To make it run, first a rabbitmq service must exists in the address defined in the environment variables called BROKER_HOSTNAME and BROKER_PORT. Default is localhost for the hostname and 5672 for the port. An example of valid URL could be amqp://guest:guest@localhost:5672, where guest:guest is the pair user:password that also can be changed in the environment variables BROKER_DEFAULT_USER and BROKER_DEFAULT_PASSWORD.

During the development of the adapter we've been using the docker image called rabbitmq:3-management to create a local docker container with a rabbitmq service and also a web interface to check the results. This image can be executed by running sh rabbitmq.sh start (or if you want to delete any previous existing container, just run sh rabbitmq.sh clean) and having docker installed. After this, to see the results you can enter in the address http://localhost:15672 or http://${BROKER_HOSTNAME}:1${BROKER_PORT} and enter the defined user and password to see the admin interface of the service.


How to test it

For this you will need two terminals. In one, run consumer.ts, in the other run producer.ts. Make sure the environment variables are correctly set. The expected result is something like: Action on message {"mseconds":16} one per second.