File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change 5
5
"main" : " send.js" ,
6
6
"scripts" : {
7
7
"test" : " echo \" Error: no test specified\" && exit 1" ,
8
- "start" : " node send.js && receive.js"
8
+ "producer" : " node src/send.js" ,
9
+ "consumer" : " node src/receive.js"
9
10
},
10
11
"keywords" : [],
11
12
"author" : " " ,
Original file line number Diff line number Diff line change
1
+ const amqp = require ( 'amqplib/callback_api' )
2
+
3
+ amqp . connect ( 'amqp://localhost' , ( err , conn ) => { // A configuração é a mesma que o send.js
4
+ if ( err ) throw err
5
+
6
+ conn . createChannel ( ( err , channel ) => {
7
+ if ( err ) throw err
8
+
9
+ // Declarando a queue no consumer
10
+ const queue = 'hello'
11
+
12
+ channel . assertQueue ( queue , {
13
+ durable : false
14
+ } )
15
+
16
+ console . log ( ` [*] Waiting for messages in ${ queue } . To exit press CTRL+C` )
17
+
18
+ channel . consume ( queue , ( message ) => { // Funciona de forma assíncrona, espera a mensage e só exibe assim que o RabbitMQ enviar para o consumer
19
+ console . log ( ` [x] Received: ${ message . content . toString ( ) } ` )
20
+ } , {
21
+ noAck : true
22
+ } )
23
+ } )
24
+ } )
Original file line number Diff line number Diff line change
1
+ const amqp = require ( 'amqplib/callback_api' )
2
+
3
+ amqp . connect ( 'amqp://localhost' , ( err , conn ) => { // Criando conexão no servidor
4
+ if ( err ) throw err
5
+
6
+ conn . createChannel ( ( err , channel ) => { // Cria canal para desenvolver a maior parte da API
7
+ if ( err ) throw err
8
+
9
+ // Declarando queue/fila
10
+ const queue = 'hello'
11
+ const message = 'Hello World!'
12
+
13
+ channel . assertQueue ( queue , {
14
+ durable : false
15
+ } )
16
+
17
+ channel . sendToQueue ( queue , Buffer . from ( message ) ) // O conteúdo é um array de bytes
18
+ console . log ( ` [x] Sent: ${ message } ` )
19
+ } )
20
+
21
+ setTimeout ( ( ) => {
22
+ conn . close ( )
23
+ process . exit ( 0 )
24
+ } , 500 ) // Fecha conexão
25
+ } )
You can’t perform that action at this time.
0 commit comments