@inteli.city/node-red-contrib-rabbit-mq 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +239 -0
- package/mq.ackw.html +49 -0
- package/mq.ackw.js +50 -0
- package/mq.consumer.html +105 -0
- package/mq.consumer.js +259 -0
- package/mq.producer.html +104 -0
- package/mq.producer.js +181 -0
- package/mq.store.js +8 -0
- package/package.json +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# @inteli.city/node-red-contrib-rabbit-mq
|
|
2
|
+
|
|
3
|
+
Node-RED nodes for consuming, producing, and acknowledging RabbitMQ messages via AMQP.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Overview](#overview)
|
|
10
|
+
- [Nodes](#nodes)
|
|
11
|
+
- [mq.consumer](#mqconsumer)
|
|
12
|
+
- [mq.ackw](#mqackw)
|
|
13
|
+
- [mq.producer](#mqproducer)
|
|
14
|
+
- [Basic Flow](#basic-flow)
|
|
15
|
+
- [System Behavior](#system-behavior)
|
|
16
|
+
- [Important Decisions](#important-decisions)
|
|
17
|
+
- [Common Issues](#common-issues)
|
|
18
|
+
- [Debug](#debug)
|
|
19
|
+
- [Simple Example](#simple-example)
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Overview
|
|
24
|
+
|
|
25
|
+
These three nodes implement the **consume → process → ack** pattern for RabbitMQ.
|
|
26
|
+
|
|
27
|
+
- `mq.consumer` connects to a queue and emits messages into the Node-RED flow
|
|
28
|
+
- `mq.ackw` acknowledges the message after processing — **required**
|
|
29
|
+
- `mq.producer` publishes messages to an exchange with a routing key
|
|
30
|
+
|
|
31
|
+
The consumer operates in manual ack mode. RabbitMQ only removes a message from the queue when `mq.ackw` is called. Without an ack, the message stays in-flight until the timeout fires and is then requeued.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Nodes
|
|
36
|
+
|
|
37
|
+
### mq.consumer
|
|
38
|
+
|
|
39
|
+
Connects to a RabbitMQ queue and emits one message per delivery.
|
|
40
|
+
|
|
41
|
+
#### Parameters
|
|
42
|
+
|
|
43
|
+
| Field | Description |
|
|
44
|
+
|---|---|
|
|
45
|
+
| Host | Broker address |
|
|
46
|
+
| User / Password | AMQP credentials |
|
|
47
|
+
| Queue | Queue name (asserted as durable on connect) |
|
|
48
|
+
| Prefetch | Maximum number of unacknowledged messages at once |
|
|
49
|
+
| Timeout (min) | Maximum time before automatic nack. `0` uses the 5-minute floor |
|
|
50
|
+
| SSL/TLS | Use `amqps://` — required for cloud brokers |
|
|
51
|
+
| Debug mode | Enables verbose logs |
|
|
52
|
+
|
|
53
|
+
#### Behavior
|
|
54
|
+
|
|
55
|
+
- Connects automatically on deploy
|
|
56
|
+
- On failure: reconnects with exponential backoff (1s → 2s → ... → 60s)
|
|
57
|
+
- **Prefetch** limits parallelism: with `prefetch=1`, the consumer only receives the next message after acking the current one
|
|
58
|
+
- **Timeout**: if the flow does not call `mq.ackw` within the configured window, the message is automatically nacked and requeued
|
|
59
|
+
|
|
60
|
+
#### Output
|
|
61
|
+
|
|
62
|
+
Each emitted message contains:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
msg.payload → message content (JSON or string)
|
|
66
|
+
msg.rabbitmq → internal context (do not modify)
|
|
67
|
+
msg._msgid → delivery identifier
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
> **Warning:** `msg.rabbitmq` must be passed intact to `mq.ackw`. Any node that recreates the `msg` object (e.g. `return { payload: ... }`) will destroy this context.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
### mq.ackw
|
|
75
|
+
|
|
76
|
+
Acknowledges a message to RabbitMQ.
|
|
77
|
+
|
|
78
|
+
#### When to use
|
|
79
|
+
|
|
80
|
+
Always, after processing a message consumed by `mq.consumer`.
|
|
81
|
+
|
|
82
|
+
#### What it does
|
|
83
|
+
|
|
84
|
+
- Calls `channel.ack(message)` on the broker
|
|
85
|
+
- Cancels the message timeout timer
|
|
86
|
+
- Passes `msg` downstream (nodes can be chained after the ack)
|
|
87
|
+
|
|
88
|
+
#### Consequences of not using it
|
|
89
|
+
|
|
90
|
+
- The message remains "unacked" in RabbitMQ
|
|
91
|
+
- The prefetch window fills up after N messages (where N = configured prefetch)
|
|
92
|
+
- The consumer **silently stops receiving new messages**
|
|
93
|
+
- After the timeout, the message is nacked and requeued — the cycle repeats
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### mq.producer
|
|
98
|
+
|
|
99
|
+
Publishes messages to a RabbitMQ exchange.
|
|
100
|
+
|
|
101
|
+
#### Parameters
|
|
102
|
+
|
|
103
|
+
| Field | Description |
|
|
104
|
+
|---|---|
|
|
105
|
+
| Host | Broker address |
|
|
106
|
+
| User / Password | AMQP credentials |
|
|
107
|
+
| Exchange | Exchange name (asserted as `direct` and durable on connect) |
|
|
108
|
+
| Routing Key | Default routing key |
|
|
109
|
+
| SSL/TLS | Use `amqps://` — required for cloud brokers |
|
|
110
|
+
| Debug mode | Enables publish logs |
|
|
111
|
+
|
|
112
|
+
#### Connection behavior
|
|
113
|
+
|
|
114
|
+
- Connects on deploy. On failure: exponential backoff (1s → 30s)
|
|
115
|
+
- Uses a **confirm channel**: only advances in the flow after the broker confirms receipt
|
|
116
|
+
- `msg.exchange` and `msg.routingKey` override the configured values per message
|
|
117
|
+
|
|
118
|
+
#### When disconnected
|
|
119
|
+
|
|
120
|
+
> **Messages are dropped.** If the producer is not connected when a message arrives, the message is lost and a `warn` is written to the logs.
|
|
121
|
+
|
|
122
|
+
There is no internal buffer. Messages arriving during reconnection are discarded.
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Basic Flow
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
mq.consumer → [processing] → mq.ackw
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
1. `mq.consumer` receives the message and emits it
|
|
133
|
+
2. The flow processes it (function node, HTTP request, database, etc.)
|
|
134
|
+
3. `mq.ackw` confirms to RabbitMQ that the message was handled
|
|
135
|
+
|
|
136
|
+
**Ack is mandatory.** Without it:
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
message delivered → not acked → timeout (≥5 min) → nack → requeued → delivered again
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## System Behavior
|
|
145
|
+
|
|
146
|
+
**Automatic reconnect**
|
|
147
|
+
All nodes reconnect automatically with exponential backoff. Restarting Node-RED is not required.
|
|
148
|
+
|
|
149
|
+
**Prefetch**
|
|
150
|
+
Controls how many messages can be in-flight simultaneously per consumer. With `prefetch=1`, the flow is strictly sequential. With `prefetch=N`, up to N messages can be processed in parallel — but all must be acked.
|
|
151
|
+
|
|
152
|
+
**Blocking due to missing ack**
|
|
153
|
+
If all prefetch slots are occupied by unacked messages, RabbitMQ stops delivering new messages to that consumer. No error is raised — the consumer simply goes silent.
|
|
154
|
+
|
|
155
|
+
**Redelivery**
|
|
156
|
+
Nacked or timed-out messages are requeued and redelivered. The field `msg.rabbitmq.message.fields.redelivered` indicates whether a message is a redelivery.
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Important Decisions
|
|
161
|
+
|
|
162
|
+
- **The producer drops messages when disconnected.** There is no internal buffer. If guaranteed delivery is required, implement persistence in the flow before the producer.
|
|
163
|
+
|
|
164
|
+
- **The system is at-least-once.** The same message may be delivered more than once (after nack, reconnection, or timeout). Flows should be idempotent or detect duplicates.
|
|
165
|
+
|
|
166
|
+
- **Ack is the flow's responsibility.** `mq.consumer` does not ack automatically. Every path through the flow must reach `mq.ackw` — including error paths.
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## Common Issues
|
|
171
|
+
|
|
172
|
+
- **Consumer stopped receiving messages**
|
|
173
|
+
→ Prefetch is saturated. Most common cause: `mq.ackw` is not being reached in the flow (disconnected node, silent error, or `msg.rabbitmq` destroyed by a function node).
|
|
174
|
+
|
|
175
|
+
- **Duplicate messages**
|
|
176
|
+
→ Expected behavior. Indicates a nack or reconnection occurred before the ack.
|
|
177
|
+
|
|
178
|
+
- **Messages disappearing in the producer**
|
|
179
|
+
→ The producer was disconnected. Messages arriving during reconnection are discarded.
|
|
180
|
+
|
|
181
|
+
- **`msg.rabbitmq` is undefined in mq.ackw**
|
|
182
|
+
→ An upstream node recreated the `msg` object without preserving `msg.rabbitmq`. Fix by using `msg.payload = ...; return msg;` instead of `return { payload: ... }`.
|
|
183
|
+
|
|
184
|
+
- **Consumer connects but receives no messages after restart**
|
|
185
|
+
→ Check if the queue has messages with "unacked" status from a previous session. They will be requeued once the old connection expires on the broker.
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Debug
|
|
190
|
+
|
|
191
|
+
Debug mode is enabled via checkbox in `mq.consumer` and `mq.producer`.
|
|
192
|
+
|
|
193
|
+
When enabled, the following logs appear:
|
|
194
|
+
|
|
195
|
+
**mq.consumer:**
|
|
196
|
+
- Connection details (URL, channel, queue, consumerTag)
|
|
197
|
+
- Each message received (msgid, deliveryTag)
|
|
198
|
+
- Inflight size and prefetch state
|
|
199
|
+
- 10s timer to detect flows missing an ack
|
|
200
|
+
|
|
201
|
+
**mq.producer:**
|
|
202
|
+
- Connection established
|
|
203
|
+
- Each publish (exchange, routing key)
|
|
204
|
+
|
|
205
|
+
**When to enable:**
|
|
206
|
+
- To investigate why the consumer stopped receiving messages
|
|
207
|
+
- To confirm that messages are being published
|
|
208
|
+
- To trace the lifecycle of a specific message
|
|
209
|
+
|
|
210
|
+
**Disable in production.** Generates one log entry per message, creating excessive noise and unnecessary I/O.
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## Simple Example
|
|
215
|
+
|
|
216
|
+
### Publishing messages
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
[inject (repeat: 1s)] → [mq.producer]
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Configure `mq.producer`:
|
|
223
|
+
- Exchange: `X.Teste`
|
|
224
|
+
- Routing Key: `R.Teste`
|
|
225
|
+
- SSL: enabled
|
|
226
|
+
|
|
227
|
+
### Consuming and acknowledging messages
|
|
228
|
+
|
|
229
|
+
```
|
|
230
|
+
[mq.consumer] → [debug] → [mq.ackw]
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Configure `mq.consumer`:
|
|
234
|
+
- Queue: `X.Teste`
|
|
235
|
+
- Prefetch: `1`
|
|
236
|
+
- Timeout: `0` (uses the 5-minute floor)
|
|
237
|
+
- SSL: enabled
|
|
238
|
+
|
|
239
|
+
The `debug` node displays `msg.payload`. The `mq.ackw` confirms to the broker. Without `mq.ackw` at the end, the consumer will stall after the first message (prefetch=1).
|
package/mq.ackw.html
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<!-- Edit panel template -->
|
|
2
|
+
<script type="text/html" data-template-name="mq.ackw">
|
|
3
|
+
<div class="form-row">
|
|
4
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
5
|
+
<input type="text" id="node-input-name" placeholder="name">
|
|
6
|
+
</div>
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<!-- Help panel -->
|
|
10
|
+
<script type="text/html" data-help-name="mq.ackw">
|
|
11
|
+
<p>Acknowledges a RabbitMQ message previously emitted by a <code>mq.consumer</code> node.</p>
|
|
12
|
+
|
|
13
|
+
<h3>Input</h3>
|
|
14
|
+
<dl class="message-properties">
|
|
15
|
+
<dt>rabbitmq <span class="property-type">object</span></dt>
|
|
16
|
+
<dd>Must contain <code>_msgid</code> – provided automatically by the consumer node.</dd>
|
|
17
|
+
</dl>
|
|
18
|
+
|
|
19
|
+
<h3>Output</h3>
|
|
20
|
+
<p>Passes the message downstream unchanged after a successful ack, so you can chain further processing after acknowledgement.</p>
|
|
21
|
+
|
|
22
|
+
<h3>Details</h3>
|
|
23
|
+
<ul>
|
|
24
|
+
<li>Calls <code>channel.ack(message)</code> on the underlying AMQP channel.</li>
|
|
25
|
+
<li>Cancels the per-message timeout configured on the consumer.</li>
|
|
26
|
+
<li>Logs a critical error if the message context is missing or already released.</li>
|
|
27
|
+
</ul>
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<!-- Node registration -->
|
|
31
|
+
<script type="text/javascript">
|
|
32
|
+
RED.nodes.registerType('mq.ackw', {
|
|
33
|
+
category: 'network',
|
|
34
|
+
color: '#F7D4B5',
|
|
35
|
+
defaults: {
|
|
36
|
+
name: { value: '' }
|
|
37
|
+
},
|
|
38
|
+
inputs: 1,
|
|
39
|
+
outputs: 1,
|
|
40
|
+
icon: 'rabbit.svg',
|
|
41
|
+
paletteLabel: 'mq.ackw',
|
|
42
|
+
label: function () {
|
|
43
|
+
return this.name || 'mq.ackw';
|
|
44
|
+
},
|
|
45
|
+
labelStyle: function () {
|
|
46
|
+
return this.name ? 'node_label_italic' : '';
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
</script>
|
package/mq.ackw.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function (RED) {
|
|
4
|
+
const inflight = require('./mq.store');
|
|
5
|
+
|
|
6
|
+
function MQAckNode(config) {
|
|
7
|
+
RED.nodes.createNode(this, config);
|
|
8
|
+
const node = this;
|
|
9
|
+
|
|
10
|
+
node.on('input', function (msg, send, done) {
|
|
11
|
+
send = send || function () { node.send.apply(node, arguments); };
|
|
12
|
+
done = done || function (err) { if (err) node.error(err, msg); };
|
|
13
|
+
|
|
14
|
+
if (!msg.rabbitmq || !msg.rabbitmq._msgid) {
|
|
15
|
+
node.error('[ack-guard] CRITICAL: msg.rabbitmq._msgid missing – rabbitmq context was lost in the flow (check function/change nodes upstream)', msg);
|
|
16
|
+
done();
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const msgid = msg.rabbitmq._msgid;
|
|
21
|
+
const entry = inflight.get(msgid);
|
|
22
|
+
|
|
23
|
+
if (!entry) {
|
|
24
|
+
node.error(`[ack-guard] CRITICAL: missing inflight entry for msgid=${msgid} – message was lost or already released (timed out / double-acked?)`, msg);
|
|
25
|
+
done();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (entry.timeout) {
|
|
30
|
+
clearTimeout(entry.timeout);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
entry.channel.ack(entry.message);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
node.error(`[ack] ack failed for msgid=${msgid}: ${e.message}`, msg);
|
|
37
|
+
inflight.delete(msgid);
|
|
38
|
+
done(e);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
inflight.delete(msgid);
|
|
43
|
+
|
|
44
|
+
send(msg);
|
|
45
|
+
done();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
RED.nodes.registerType('mq.ackw', MQAckNode);
|
|
50
|
+
};
|
package/mq.consumer.html
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<!-- Edit panel template -->
|
|
2
|
+
<script type="text/html" data-template-name="mq.consumer">
|
|
3
|
+
<div class="form-row">
|
|
4
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
5
|
+
<input type="text" id="node-input-name" placeholder="name">
|
|
6
|
+
</div>
|
|
7
|
+
<div class="form-row">
|
|
8
|
+
<label for="node-input-host"><i class="fa fa-server"></i> Host</label>
|
|
9
|
+
<input type="text" id="node-input-host" placeholder="localhost">
|
|
10
|
+
</div>
|
|
11
|
+
<div class="form-row">
|
|
12
|
+
<label for="node-input-user"><i class="fa fa-user"></i> User</label>
|
|
13
|
+
<input type="text" id="node-input-user" placeholder="guest">
|
|
14
|
+
</div>
|
|
15
|
+
<div class="form-row">
|
|
16
|
+
<label for="node-input-password"><i class="fa fa-lock"></i> Password</label>
|
|
17
|
+
<input type="password" id="node-input-password" placeholder="guest">
|
|
18
|
+
</div>
|
|
19
|
+
<div class="form-row">
|
|
20
|
+
<label for="node-input-queue"><i class="fa fa-inbox"></i> Queue</label>
|
|
21
|
+
<input type="text" id="node-input-queue" placeholder="my-queue">
|
|
22
|
+
</div>
|
|
23
|
+
<div class="form-row">
|
|
24
|
+
<label for="node-input-prefetch"><i class="fa fa-tachometer"></i> Prefetch</label>
|
|
25
|
+
<input type="number" id="node-input-prefetch" min="1" placeholder="1" style="width:80px">
|
|
26
|
+
</div>
|
|
27
|
+
<div class="form-row">
|
|
28
|
+
<label for="node-input-timeout"><i class="fa fa-clock-o"></i> Timeout (min)</label>
|
|
29
|
+
<input type="number" id="node-input-timeout" min="0" step="0.1" placeholder="0 = 5 min floor" style="width:140px">
|
|
30
|
+
</div>
|
|
31
|
+
<div class="form-row">
|
|
32
|
+
<label for="node-input-ssl"><i class="fa fa-shield"></i> SSL/TLS</label>
|
|
33
|
+
<input type="checkbox" id="node-input-ssl" style="width:auto; margin-top:4px">
|
|
34
|
+
</div>
|
|
35
|
+
<div class="form-row">
|
|
36
|
+
<label for="node-input-debug"><i class="fa fa-bug"></i> Debug mode</label>
|
|
37
|
+
<input type="checkbox" id="node-input-debug" style="width:auto; margin-top:4px">
|
|
38
|
+
<label for="node-input-debug" style="width:auto"> (verbose – disable in production)</label>
|
|
39
|
+
</div>
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
<!-- Help panel -->
|
|
43
|
+
<script type="text/html" data-help-name="mq.consumer">
|
|
44
|
+
<p>Connects to a RabbitMQ queue and emits one message per delivery.</p>
|
|
45
|
+
|
|
46
|
+
<h3>Configuration</h3>
|
|
47
|
+
<dl class="message-properties">
|
|
48
|
+
<dt>Host</dt>
|
|
49
|
+
<dd>RabbitMQ hostname or IP address.</dd>
|
|
50
|
+
<dt>User / Password</dt>
|
|
51
|
+
<dd>AMQP credentials (stored securely via Node-RED credential store).</dd>
|
|
52
|
+
<dt>Queue</dt>
|
|
53
|
+
<dd>Name of the queue to consume. The queue is asserted as durable on connect.</dd>
|
|
54
|
+
<dt>Prefetch</dt>
|
|
55
|
+
<dd>Maximum number of unacknowledged messages delivered to this consumer at once.</dd>
|
|
56
|
+
<dt>Timeout (min)</dt>
|
|
57
|
+
<dd>If a message is not acknowledged within this many minutes it will be negatively acknowledged (nack) and requeued. Set to 0 to use the built-in 5-minute floor — timeout cannot be fully disabled.</dd>
|
|
58
|
+
<dt>SSL/TLS</dt>
|
|
59
|
+
<dd>Enable to use <code>amqps://</code> (TLS-secured connection). Disable only for local or non-secure RabbitMQ instances.</dd>
|
|
60
|
+
</dl>
|
|
61
|
+
|
|
62
|
+
<h3>Output</h3>
|
|
63
|
+
<dl class="message-properties">
|
|
64
|
+
<dt>payload <span class="property-type">object | string</span></dt>
|
|
65
|
+
<dd>Parsed JSON body of the message, or a raw string if parsing fails.</dd>
|
|
66
|
+
<dt>rabbitmq <span class="property-type">object</span></dt>
|
|
67
|
+
<dd>Internal context needed by the <code>mq.ackw</code> node. Do not modify.</dd>
|
|
68
|
+
<dt>_msgid <span class="property-type">string</span></dt>
|
|
69
|
+
<dd>Unique message identifier used to correlate acknowledgements.</dd>
|
|
70
|
+
</dl>
|
|
71
|
+
|
|
72
|
+
<h3>Connection behaviour</h3>
|
|
73
|
+
<p>On connection failure the node automatically reconnects using exponential backoff (1 s → 2 s → … → 60 s max).</p>
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<!-- Node registration -->
|
|
77
|
+
<script type="text/javascript">
|
|
78
|
+
RED.nodes.registerType('mq.consumer', {
|
|
79
|
+
category: 'network',
|
|
80
|
+
color: '#F7D4B5',
|
|
81
|
+
defaults: {
|
|
82
|
+
name: { value: '' },
|
|
83
|
+
host: { value: 'localhost', required: true },
|
|
84
|
+
queue: { value: '', required: true },
|
|
85
|
+
prefetch: { value: 1 },
|
|
86
|
+
timeout: { value: 0 },
|
|
87
|
+
ssl: { value: true },
|
|
88
|
+
debug: { value: false }
|
|
89
|
+
},
|
|
90
|
+
credentials: {
|
|
91
|
+
user: { type: 'text' },
|
|
92
|
+
password: { type: 'password' }
|
|
93
|
+
},
|
|
94
|
+
inputs: 0,
|
|
95
|
+
outputs: 1,
|
|
96
|
+
icon: 'rabbit.svg',
|
|
97
|
+
paletteLabel: 'mq.consumer',
|
|
98
|
+
label: function () {
|
|
99
|
+
return this.name || 'mq.consumer';
|
|
100
|
+
},
|
|
101
|
+
labelStyle: function () {
|
|
102
|
+
return this.name ? 'node_label_italic' : '';
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
</script>
|
package/mq.consumer.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function (RED) {
|
|
4
|
+
const amqp = require('amqplib');
|
|
5
|
+
const inflight = require('./mq.store');
|
|
6
|
+
|
|
7
|
+
// Safety floor: never allow the timeout to be fully disabled.
|
|
8
|
+
// A flow misconfiguration (missing ack node) would otherwise permanently
|
|
9
|
+
// exhaust the prefetch window and silently deadlock the consumer.
|
|
10
|
+
const MIN_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
|
|
11
|
+
const FLOW_CHECK_DELAY = 10 * 1000; // warn after 10 s if still unacked
|
|
12
|
+
|
|
13
|
+
function MQConsumerNode(config) {
|
|
14
|
+
RED.nodes.createNode(this, config);
|
|
15
|
+
const node = this;
|
|
16
|
+
|
|
17
|
+
const host = config.host;
|
|
18
|
+
const queue = config.queue;
|
|
19
|
+
const prefetch = parseInt(config.prefetch, 10) || 1;
|
|
20
|
+
|
|
21
|
+
// If timeout is 0 (disabled), enforce the minimum.
|
|
22
|
+
// This ensures no message can permanently block the channel.
|
|
23
|
+
const configuredMs = (parseFloat(config.timeout) || 0) * 60 * 1000;
|
|
24
|
+
const timeoutMs = configuredMs > 0 ? configuredMs : MIN_TIMEOUT_MS;
|
|
25
|
+
|
|
26
|
+
const debug = config.debug === true || config.debug === 'true';
|
|
27
|
+
|
|
28
|
+
const ssl = config.ssl !== false && config.ssl !== 'false';
|
|
29
|
+
|
|
30
|
+
const user = node.credentials.user || '';
|
|
31
|
+
const password = node.credentials.password || '';
|
|
32
|
+
|
|
33
|
+
let connection = null;
|
|
34
|
+
let channel = null;
|
|
35
|
+
let reconnectTimer = null;
|
|
36
|
+
let reconnectDelay = 1000;
|
|
37
|
+
let closing = false;
|
|
38
|
+
|
|
39
|
+
if (debug) node.debug(`[consumer-debug] timeout active: ${timeoutMs}ms`);
|
|
40
|
+
|
|
41
|
+
// ------------------------------------------------------------------ //
|
|
42
|
+
// Connection logic
|
|
43
|
+
// ------------------------------------------------------------------ //
|
|
44
|
+
|
|
45
|
+
async function connect() {
|
|
46
|
+
if (closing) return;
|
|
47
|
+
|
|
48
|
+
node.status({ fill: 'yellow', shape: 'ring', text: 'connecting…' });
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const protocol = ssl ? 'amqps' : 'amqp';
|
|
52
|
+
if (debug) node.debug(`[consumer-debug] connecting – ${protocol}://${encodeURIComponent(user)}@${host} queue="${queue}"`);
|
|
53
|
+
|
|
54
|
+
const url = `${protocol}://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${host}`;
|
|
55
|
+
connection = await amqp.connect(url);
|
|
56
|
+
if (debug) node.debug(`[consumer-debug] connection established`);
|
|
57
|
+
|
|
58
|
+
connection.on('error', (err) => {
|
|
59
|
+
node.warn(`[consumer] connection error: ${err.message}`);
|
|
60
|
+
connection = null;
|
|
61
|
+
channel = null;
|
|
62
|
+
scheduleReconnect();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
connection.on('close', () => {
|
|
66
|
+
if (!closing) {
|
|
67
|
+
node.warn(`[consumer] connection closed unexpectedly, scheduling reconnect`);
|
|
68
|
+
connection = null;
|
|
69
|
+
channel = null;
|
|
70
|
+
scheduleReconnect();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
channel = await connection.createChannel();
|
|
75
|
+
if (debug) node.debug(`[consumer-debug] channel created`);
|
|
76
|
+
|
|
77
|
+
channel.on('error', (err) => {
|
|
78
|
+
node.warn(`[consumer] channel error: ${err.message}`);
|
|
79
|
+
channel = null;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
channel.on('close', () => {
|
|
83
|
+
node.warn(`[consumer] channel closed`);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const queueInfo = await channel.assertQueue(queue, { durable: true });
|
|
87
|
+
if (debug) node.debug(`[consumer-debug] queue asserted: "${queue}" – messages=${queueInfo.messageCount} consumers=${queueInfo.consumerCount}`);
|
|
88
|
+
|
|
89
|
+
channel.prefetch(prefetch);
|
|
90
|
+
if (debug) node.debug(`[consumer-debug] prefetch set: ${prefetch}`);
|
|
91
|
+
|
|
92
|
+
reconnectDelay = 1000;
|
|
93
|
+
node.status({ fill: 'green', shape: 'dot', text: `connected: ${queue}` });
|
|
94
|
+
|
|
95
|
+
const consumeResult = await channel.consume(queue, (msg) => {
|
|
96
|
+
if (msg === null) {
|
|
97
|
+
node.warn(`[consumer] consumer cancelled by broker on queue="${queue}"`);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
handleMessage(msg);
|
|
101
|
+
}, { noAck: false });
|
|
102
|
+
|
|
103
|
+
if (debug) node.debug(`[consumer-debug] consuming – consumerTag="${consumeResult.consumerTag}"`);
|
|
104
|
+
|
|
105
|
+
} catch (err) {
|
|
106
|
+
node.error(`[consumer] failed to connect: ${err.message}`);
|
|
107
|
+
node.status({ fill: 'red', shape: 'ring', text: `error: ${err.message}` });
|
|
108
|
+
scheduleReconnect();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Discard all inflight entries that belong to a given channel.
|
|
113
|
+
// Must be called before nulling the channel reference so the match works.
|
|
114
|
+
// Cancels every pending timeout so no deferred action fires against a
|
|
115
|
+
// dead channel after RabbitMQ has already requeued those messages.
|
|
116
|
+
function clearInflightForChannel(ch) {
|
|
117
|
+
if (!ch) return;
|
|
118
|
+
let count = 0;
|
|
119
|
+
for (const [msgid, entry] of inflight.entries()) {
|
|
120
|
+
if (entry.channel === ch) {
|
|
121
|
+
if (entry.timeout) clearTimeout(entry.timeout);
|
|
122
|
+
inflight.delete(msgid);
|
|
123
|
+
count++;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (count > 0) {
|
|
127
|
+
node.warn(`[consumer] discarded ${count} inflight message(s) – connection lost, RabbitMQ will requeue them`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function scheduleReconnect() {
|
|
132
|
+
if (closing) return;
|
|
133
|
+
clearInflightForChannel(channel); // reset before nulling refs
|
|
134
|
+
connection = null;
|
|
135
|
+
channel = null;
|
|
136
|
+
|
|
137
|
+
node.status({ fill: 'yellow', shape: 'ring', text: `reconnecting in ${reconnectDelay / 1000}s…` });
|
|
138
|
+
reconnectTimer = setTimeout(() => {
|
|
139
|
+
reconnectDelay = Math.min(reconnectDelay * 2, 60000);
|
|
140
|
+
connect();
|
|
141
|
+
}, reconnectDelay);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ------------------------------------------------------------------ //
|
|
145
|
+
// Message handling
|
|
146
|
+
// ------------------------------------------------------------------ //
|
|
147
|
+
|
|
148
|
+
function handleMessage(msg) {
|
|
149
|
+
const msgid = RED.util.generateId();
|
|
150
|
+
|
|
151
|
+
let payload;
|
|
152
|
+
try {
|
|
153
|
+
payload = JSON.parse(msg.content.toString());
|
|
154
|
+
} catch (e) {
|
|
155
|
+
node.warn(`[consumer] JSON parse failed, using raw string: ${e.message}`);
|
|
156
|
+
payload = msg.content.toString();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Safety-net timeout: nack and requeue if the flow never reaches
|
|
160
|
+
// the ack node within the configured window.
|
|
161
|
+
const timeoutHandle = setTimeout(() => {
|
|
162
|
+
const entry = inflight.get(msgid);
|
|
163
|
+
if (!entry) return;
|
|
164
|
+
|
|
165
|
+
// Only nack if the channel is still the same live channel.
|
|
166
|
+
// If the connection was lost, clearInflightForChannel() will
|
|
167
|
+
// have already removed this entry — but under a race condition
|
|
168
|
+
// the timeout may fire just before or just after that cleanup.
|
|
169
|
+
// In either case, acting on a closed/replaced channel is wrong.
|
|
170
|
+
if (!channel || entry.channel !== channel) {
|
|
171
|
+
node.warn(`[consumer-guard] timeout for msgid=${msgid} skipped – channel no longer valid, RabbitMQ already requeued`);
|
|
172
|
+
inflight.delete(msgid);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
node.warn(`[consumer-guard] auto-nack triggered for msgid=${msgid} – flow did not ack in time (requeue=true)`);
|
|
177
|
+
try {
|
|
178
|
+
entry.channel.nack(entry.message, false, true);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
node.error(`[consumer-guard] auto-nack error for msgid=${msgid}: ${e.message}`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
inflight.delete(msgid);
|
|
184
|
+
}, timeoutMs);
|
|
185
|
+
|
|
186
|
+
inflight.set(msgid, {
|
|
187
|
+
channel,
|
|
188
|
+
message: msg,
|
|
189
|
+
timeout: timeoutHandle
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// ---- Observability guards ----------------------------------------
|
|
193
|
+
|
|
194
|
+
if (debug) {
|
|
195
|
+
node.debug(`[consumer-debug] inflight size=${inflight.size}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (debug && inflight.size >= prefetch) {
|
|
199
|
+
node.debug(`[consumer-debug] prefetch limit reached (inflight=${inflight.size} prefetch=${prefetch}) – normal backpressure behavior`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Early-warning: only active in debug mode to avoid per-message timer overhead.
|
|
203
|
+
if (debug) {
|
|
204
|
+
setTimeout(() => {
|
|
205
|
+
if (inflight.has(msgid)) {
|
|
206
|
+
node.error(`[consumer-guard] message ${msgid} still unacked after ${FLOW_CHECK_DELAY / 1000}s – flow likely not reaching ack node`);
|
|
207
|
+
}
|
|
208
|
+
}, FLOW_CHECK_DELAY);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (debug) node.debug(`[consumer-debug] message received – msgid=${msgid} deliveryTag=${msg.fields.deliveryTag}`);
|
|
212
|
+
|
|
213
|
+
const outMsg = {
|
|
214
|
+
payload,
|
|
215
|
+
_msgid: msgid,
|
|
216
|
+
rabbitmq: {
|
|
217
|
+
message: msg,
|
|
218
|
+
_msgid: msgid
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
node.send(outMsg);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ------------------------------------------------------------------ //
|
|
226
|
+
// Cleanup on node close / redeploy
|
|
227
|
+
// ------------------------------------------------------------------ //
|
|
228
|
+
|
|
229
|
+
node.on('close', async (done) => {
|
|
230
|
+
closing = true;
|
|
231
|
+
|
|
232
|
+
if (reconnectTimer) {
|
|
233
|
+
clearTimeout(reconnectTimer);
|
|
234
|
+
reconnectTimer = null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
clearInflightForChannel(channel);
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
if (channel) await channel.close().catch(() => {});
|
|
241
|
+
if (connection) await connection.close().catch(() => {});
|
|
242
|
+
} catch (e) {
|
|
243
|
+
node.warn(`[consumer] error during close: ${e.message}`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
node.status({});
|
|
247
|
+
done();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
connect();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
RED.nodes.registerType('mq.consumer', MQConsumerNode, {
|
|
254
|
+
credentials: {
|
|
255
|
+
user: { type: 'text' },
|
|
256
|
+
password: { type: 'password' }
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
};
|
package/mq.producer.html
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
<!-- Edit panel template -->
|
|
2
|
+
<script type="text/html" data-template-name="mq.producer">
|
|
3
|
+
<div class="form-row">
|
|
4
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
5
|
+
<input type="text" id="node-input-name" placeholder="name">
|
|
6
|
+
</div>
|
|
7
|
+
<div class="form-row">
|
|
8
|
+
<label for="node-input-host"><i class="fa fa-server"></i> Host</label>
|
|
9
|
+
<input type="text" id="node-input-host" placeholder="localhost">
|
|
10
|
+
</div>
|
|
11
|
+
<div class="form-row">
|
|
12
|
+
<label for="node-input-user"><i class="fa fa-user"></i> User</label>
|
|
13
|
+
<input type="text" id="node-input-user" placeholder="guest">
|
|
14
|
+
</div>
|
|
15
|
+
<div class="form-row">
|
|
16
|
+
<label for="node-input-password"><i class="fa fa-lock"></i> Password</label>
|
|
17
|
+
<input type="password" id="node-input-password" placeholder="guest">
|
|
18
|
+
</div>
|
|
19
|
+
<div class="form-row">
|
|
20
|
+
<label for="node-input-exchange"><i class="fa fa-random"></i> Exchange</label>
|
|
21
|
+
<input type="text" id="node-input-exchange" placeholder="my-exchange">
|
|
22
|
+
</div>
|
|
23
|
+
<div class="form-row">
|
|
24
|
+
<label for="node-input-routingKey"><i class="fa fa-key"></i> Routing Key</label>
|
|
25
|
+
<input type="text" id="node-input-routingKey" placeholder="my-routing-key">
|
|
26
|
+
</div>
|
|
27
|
+
<div class="form-row">
|
|
28
|
+
<label for="node-input-ssl"><i class="fa fa-shield"></i> SSL/TLS</label>
|
|
29
|
+
<input type="checkbox" id="node-input-ssl" style="width:auto; margin-top:4px">
|
|
30
|
+
</div>
|
|
31
|
+
<div class="form-row">
|
|
32
|
+
<label for="node-input-debug"><i class="fa fa-bug"></i> Debug mode</label>
|
|
33
|
+
<input type="checkbox" id="node-input-debug" style="width:auto; margin-top:4px">
|
|
34
|
+
<label for="node-input-debug" style="width:auto"> (verbose – disable in production)</label>
|
|
35
|
+
</div>
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
<!-- Help panel -->
|
|
39
|
+
<script type="text/html" data-help-name="mq.producer">
|
|
40
|
+
<p>Publishes messages to a RabbitMQ exchange.</p>
|
|
41
|
+
|
|
42
|
+
<h3>Configuration</h3>
|
|
43
|
+
<dl class="message-properties">
|
|
44
|
+
<dt>Host</dt>
|
|
45
|
+
<dd>RabbitMQ hostname or IP address.</dd>
|
|
46
|
+
<dt>User / Password</dt>
|
|
47
|
+
<dd>AMQP credentials (stored securely via Node-RED credential store).</dd>
|
|
48
|
+
<dt>Exchange</dt>
|
|
49
|
+
<dd>Target exchange name. Asserted as <code>direct</code> and durable on connect.</dd>
|
|
50
|
+
<dt>Routing Key</dt>
|
|
51
|
+
<dd>Default routing key used when <code>msg.routingKey</code> is not set.</dd>
|
|
52
|
+
<dt>SSL/TLS</dt>
|
|
53
|
+
<dd>Enable to use <code>amqps://</code> (TLS-secured connection). Disable only for local or non-secure RabbitMQ instances.</dd>
|
|
54
|
+
</dl>
|
|
55
|
+
|
|
56
|
+
<h3>Input</h3>
|
|
57
|
+
<dl class="message-properties">
|
|
58
|
+
<dt>payload <span class="property-type">any</span></dt>
|
|
59
|
+
<dd>Published as <code>{ data: msg.payload }</code> (JSON-encoded).</dd>
|
|
60
|
+
<dt>exchange <span class="property-type">string</span> <span class="property-optional">(optional)</span></dt>
|
|
61
|
+
<dd>Overrides the configured exchange for this message only.</dd>
|
|
62
|
+
<dt>routingKey <span class="property-type">string</span> <span class="property-optional">(optional)</span></dt>
|
|
63
|
+
<dd>Overrides the configured routing key for this message only.</dd>
|
|
64
|
+
</dl>
|
|
65
|
+
|
|
66
|
+
<h3>Output</h3>
|
|
67
|
+
<p>Forwards <code>msg</code> unchanged downstream after the broker confirms receipt.</p>
|
|
68
|
+
|
|
69
|
+
<h3>Connection behaviour</h3>
|
|
70
|
+
<p>Connects eagerly on deploy. On failure the node reconnects using exponential backoff (1 s → 2 s → … → 30 s max). Messages arriving while disconnected are dropped and logged as warnings.</p>
|
|
71
|
+
|
|
72
|
+
<h3>Publish confirmation</h3>
|
|
73
|
+
<p>Uses a <em>confirm channel</em>, so the node waits for a broker-level ack before forwarding the message downstream.</p>
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<!-- Node registration -->
|
|
77
|
+
<script type="text/javascript">
|
|
78
|
+
RED.nodes.registerType('mq.producer', {
|
|
79
|
+
category: 'network',
|
|
80
|
+
color: '#F7D4B5',
|
|
81
|
+
defaults: {
|
|
82
|
+
name: { value: '' },
|
|
83
|
+
host: { value: 'localhost', required: true },
|
|
84
|
+
exchange: { value: '', required: true },
|
|
85
|
+
routingKey: { value: '' },
|
|
86
|
+
ssl: { value: true },
|
|
87
|
+
debug: { value: false }
|
|
88
|
+
},
|
|
89
|
+
credentials: {
|
|
90
|
+
user: { type: 'text' },
|
|
91
|
+
password: { type: 'password' }
|
|
92
|
+
},
|
|
93
|
+
inputs: 1,
|
|
94
|
+
outputs: 1,
|
|
95
|
+
icon: 'rabbit.svg',
|
|
96
|
+
paletteLabel: 'mq.producer',
|
|
97
|
+
label: function () {
|
|
98
|
+
return this.name || 'mq.producer';
|
|
99
|
+
},
|
|
100
|
+
labelStyle: function () {
|
|
101
|
+
return this.name ? 'node_label_italic' : '';
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
</script>
|
package/mq.producer.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function (RED) {
|
|
4
|
+
const amqp = require('amqplib');
|
|
5
|
+
|
|
6
|
+
function MQProducerNode(config) {
|
|
7
|
+
RED.nodes.createNode(this, config);
|
|
8
|
+
const node = this;
|
|
9
|
+
|
|
10
|
+
const cfgExchange = config.exchange;
|
|
11
|
+
const cfgRoutingKey = config.routingKey;
|
|
12
|
+
const host = config.host;
|
|
13
|
+
|
|
14
|
+
const ssl = config.ssl !== false && config.ssl !== 'false';
|
|
15
|
+
const debug = config.debug === true || config.debug === 'true';
|
|
16
|
+
|
|
17
|
+
const user = node.credentials.user || '';
|
|
18
|
+
const password = node.credentials.password || '';
|
|
19
|
+
|
|
20
|
+
let connection = null;
|
|
21
|
+
let channel = null;
|
|
22
|
+
let reconnectTimer = null;
|
|
23
|
+
let reconnectDelay = 1000;
|
|
24
|
+
let closing = false;
|
|
25
|
+
let connecting = false;
|
|
26
|
+
|
|
27
|
+
// ------------------------------------------------------------------ //
|
|
28
|
+
// Connection logic
|
|
29
|
+
// ------------------------------------------------------------------ //
|
|
30
|
+
|
|
31
|
+
async function connect() {
|
|
32
|
+
if (closing || connecting) return;
|
|
33
|
+
connecting = true;
|
|
34
|
+
|
|
35
|
+
node.status({ fill: 'yellow', shape: 'dot', text: 'connecting…' });
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const protocol = ssl ? 'amqps' : 'amqp';
|
|
39
|
+
if (debug) node.debug(`[producer-debug] connecting – ${protocol}://${encodeURIComponent(user)}@${host} exchange="${cfgExchange}"`);
|
|
40
|
+
const url = `${protocol}://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${host}`;
|
|
41
|
+
connection = await amqp.connect(url);
|
|
42
|
+
|
|
43
|
+
connection.on('error', (err) => {
|
|
44
|
+
node.warn(`[producer] connection error: ${err.message}`);
|
|
45
|
+
connection = null;
|
|
46
|
+
channel = null;
|
|
47
|
+
scheduleReconnect();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
connection.on('close', () => {
|
|
51
|
+
if (!closing) {
|
|
52
|
+
node.warn('[producer] connection closed unexpectedly, scheduling reconnect');
|
|
53
|
+
connection = null;
|
|
54
|
+
channel = null;
|
|
55
|
+
scheduleReconnect();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
channel = await connection.createConfirmChannel();
|
|
60
|
+
|
|
61
|
+
channel.on('error', (err) => {
|
|
62
|
+
node.warn(`[producer] channel error: ${err.message}`);
|
|
63
|
+
channel = null;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
await channel.assertExchange(cfgExchange, 'direct', { durable: true });
|
|
67
|
+
|
|
68
|
+
connecting = false;
|
|
69
|
+
reconnectDelay = 1000;
|
|
70
|
+
node.status({ fill: 'green', shape: 'ring', text: `connected: ${cfgExchange} → ${cfgRoutingKey || '-'}` });
|
|
71
|
+
if (debug) node.debug(`[producer-debug] connected – exchange="${cfgExchange}"`);
|
|
72
|
+
|
|
73
|
+
} catch (err) {
|
|
74
|
+
connecting = false;
|
|
75
|
+
connection = null;
|
|
76
|
+
channel = null;
|
|
77
|
+
node.error(`[producer] failed to connect: ${err.message}`);
|
|
78
|
+
node.status({ fill: 'red', shape: 'ring', text: `error: ${err.message}` });
|
|
79
|
+
scheduleReconnect();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function scheduleReconnect() {
|
|
84
|
+
if (closing || reconnectTimer) return;
|
|
85
|
+
node.status({ fill: 'yellow', shape: 'dot', text: `reconnecting in ${reconnectDelay / 1000}s…` });
|
|
86
|
+
reconnectTimer = setTimeout(() => {
|
|
87
|
+
reconnectTimer = null;
|
|
88
|
+
reconnectDelay = Math.min(reconnectDelay * 2, 30000);
|
|
89
|
+
connect();
|
|
90
|
+
}, reconnectDelay);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ------------------------------------------------------------------ //
|
|
94
|
+
// Message input
|
|
95
|
+
// ------------------------------------------------------------------ //
|
|
96
|
+
|
|
97
|
+
node.on('input', async function (msg, send, done) {
|
|
98
|
+
send = send || function () { node.send.apply(node, arguments); };
|
|
99
|
+
done = done || function (err) { if (err) node.error(err, msg); };
|
|
100
|
+
|
|
101
|
+
if (!channel) {
|
|
102
|
+
if (!connecting && !reconnectTimer) connect();
|
|
103
|
+
node.warn('[producer] message dropped – producer is not connected to RabbitMQ');
|
|
104
|
+
node.status({ fill: 'red', shape: 'ring', text: 'not connected' });
|
|
105
|
+
done();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const targetExchange = msg.exchange || cfgExchange;
|
|
110
|
+
const targetRoutingKey = msg.routingKey || cfgRoutingKey;
|
|
111
|
+
|
|
112
|
+
const message = { data: msg.payload };
|
|
113
|
+
const buffer = Buffer.from(JSON.stringify(message));
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
await new Promise((resolve, reject) => {
|
|
117
|
+
const drained = channel.publish(
|
|
118
|
+
targetExchange,
|
|
119
|
+
targetRoutingKey,
|
|
120
|
+
buffer,
|
|
121
|
+
{ persistent: true },
|
|
122
|
+
(err) => { if (err) reject(err); else resolve(); }
|
|
123
|
+
);
|
|
124
|
+
if (!drained) {
|
|
125
|
+
node.warn('[producer] channel write buffer full – back-pressure applied');
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
if (debug) {
|
|
130
|
+
node.debug(`[producer-debug] published to exchange="${targetExchange}" routingKey="${targetRoutingKey}"`);
|
|
131
|
+
}
|
|
132
|
+
node.status({ fill: 'green', shape: 'ring', text: `published: ${targetExchange} → ${targetRoutingKey}` });
|
|
133
|
+
send(msg);
|
|
134
|
+
done();
|
|
135
|
+
|
|
136
|
+
setTimeout(() => {
|
|
137
|
+
if (channel) node.status({ fill: 'green', shape: 'ring', text: `connected: ${cfgExchange} → ${cfgRoutingKey || '-'}` });
|
|
138
|
+
}, 2000);
|
|
139
|
+
|
|
140
|
+
} catch (err) {
|
|
141
|
+
node.error(`[producer] publish failed: ${err.message}`, msg);
|
|
142
|
+
node.status({ fill: 'red', shape: 'ring', text: `publish error: ${err.message}` });
|
|
143
|
+
channel = null;
|
|
144
|
+
scheduleReconnect();
|
|
145
|
+
done(err);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// ------------------------------------------------------------------ //
|
|
150
|
+
// Cleanup on node close / redeploy
|
|
151
|
+
// ------------------------------------------------------------------ //
|
|
152
|
+
|
|
153
|
+
node.on('close', async (done) => {
|
|
154
|
+
closing = true;
|
|
155
|
+
|
|
156
|
+
if (reconnectTimer) {
|
|
157
|
+
clearTimeout(reconnectTimer);
|
|
158
|
+
reconnectTimer = null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
if (channel) await channel.close().catch(() => {});
|
|
163
|
+
if (connection) await connection.close().catch(() => {});
|
|
164
|
+
} catch (e) {
|
|
165
|
+
node.warn(`[producer] error during close: ${e.message}`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
node.status({});
|
|
169
|
+
done();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
connect();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
RED.nodes.registerType('mq.producer', MQProducerNode, {
|
|
176
|
+
credentials: {
|
|
177
|
+
user: { type: 'text' },
|
|
178
|
+
password: { type: 'password' }
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
};
|
package/mq.store.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@inteli.city/node-red-contrib-rabbit-mq",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Node-RED nodes for producing, consuming and acknowledging RabbitMQ messages",
|
|
5
|
+
"keywords": ["node-red", "rabbitmq", "amqp"],
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"amqplib": "^0.10.9"
|
|
9
|
+
},
|
|
10
|
+
"node-red": {
|
|
11
|
+
"version": ">=2.0.0",
|
|
12
|
+
"nodes": {
|
|
13
|
+
"mq.consumer": "mq.consumer.js",
|
|
14
|
+
"mq.ackw": "mq.ackw.js",
|
|
15
|
+
"mq.producer": "mq.producer.js"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|