-
Notifications
You must be signed in to change notification settings - Fork 3
Home
Getting everything ready
That means clone this repo and then create a Rabbitmq instance either via CloudAMQP or on your machine, make sure you have the Rabbitmq management plugin:
Clone this repo
$ git clone https://github.com/kuryaki/RabbitWorkshop.git
Install all dependencies
$ cd RabbitWorkshop
RabbitWorkshop/$ npm install
Lets explore whats already created...
Basic produce/consume
I. Add Jackrabbit (Super basic AMQP client)
const jackrabbit = require('jackrabbit');
const rabbit = jackrabbit(RABBITMQ_URL);
const exchange = rabbit.topic('jsconf');
There are 3 types of Exchanges Direct, Fanout, Topic, for the sake of the workshop we are going to use topic that can behave as both.
II. Publish the user to a key
exchange.publish(user, { key: 'register.user' })
First parameter is the payload
of the message, the second parameter is the options where one of the options is the routing key.
III. Build the service
- Make a folder named mailer
- Create an index.js file in it
- Import the rabbitmq dependencies:
const jackrabbit = require('jackrabbit');
const rabbit = jackrabbit(RABBITMQ_URL);
const exchange = rabbit.topic('jsconf');
- create the consumer:
const consumer = exchange.queue({ key: 'register.user' });
consumer.consume((data, ack) => {
console.log(`sending email to ${data.email}`);
ack();
});
- launch an instance of the consumer and an instance of the app:
node mailer
node app
- access the app at (http://localhost:3000)
- register a new user (this will queue a job)
IV. Experiment with the service
-
check the dashboard in the queues, how are they named?, how many there are? look for the one that is bind to the key
-
launch another instance of the mailer, and queue at least 3 more jobs, what happens?
-
stop all the
mailer
process and queue at least 3 more jobs, then restart the process, does it queue them? -
open the service again what happens? does it send the emails?
-
try adding a name parameter in the queue options and restart the service, check the dashboard
const consumer = exchange.queue({ key: 'register.user', name: 'mailer' });
-
do again the number 2, 3 and 4 from the list above, what happens?