@@ -45,21 +45,88 @@ RED.nodes.registerType("sample",SampleNode);
4545Nodes register a listener on the ` input ` event to receive messages from the
4646up-stream nodes in a flow.
4747
48+ With Node-RED 1.0, a new style of listener was introduced to allow the node
49+ to notify the runtime when it has finished handling a message. This added
50+ two new parameters to the listener function. Some care is needed to ensure
51+ the node can still be installed in Node-RED 0.x which does not use this new
52+ style of listener.
53+
4854{% highlight javascript %}
49- this.on('input', function(msg) {
55+ this.on('input', function(msg, send, done ) {
5056 // do something with 'msg'
57+
58+ // Once finished, call 'done'.
59+ // This call is wrapped in a check that 'done' exists
60+ // so the node will work in earlier versions of Node-RED (<1.0)
61+ if (done) {
62+ done();
63+ }
5164});
5265{% endhighlight %}
5366
67+ #### Handling errors
68+
69+ If the node encounters an error whilst handling the message, it should pass
70+ the details of the error to the ` done ` function.
71+
72+ This will trigger any Catch nodes present on the same tab, allowing the user to
73+ build flows to handle the error.
74+
75+ Again, some care is needed in the case the node is installed in Node-RED 0.x which
76+ does not provide the ` done ` function. In that case, it should use ` node.error ` :
77+
78+
79+ {% highlight javascript %}
80+ let node = this;
81+ this.on('input', function(msg, send, done) {
82+ // do something with 'msg'
83+
84+ // If an error is hit, report it to the runtime
85+ if (err) {
86+ if (done) {
87+ // Node-RED 1.0 compatible
88+ done(err);
89+ } else {
90+ // Node-RED 0.x compatible
91+ node.error(err, msg);
92+ }
93+ }
94+ });
95+ {% endhighlight %}
96+
97+
5498### Sending messages
5599
56- Nodes can send messages to the down-stream nodes in a flow using the ` send ` function:
100+ If the node sits at the start of the flow and produces messages in response to
101+ external events, it should use the ` send ` function on the Node object:
57102
58103{% highlight javascript %}
59104var msg = { payload:"hi" }
60105this.send(msg);
61106{% endhighlight %}
62107
108+ If the node wants to send from inside the ` input ` event listener, in response to
109+ receiving a message, it should use the ` send ` function that is passed to the listener
110+ function:
111+
112+ {% highlight javascript %}
113+ let node = this;
114+ this.on('input', function(msg, send, done) {
115+ // For maximum backwards compatibility, check that send exists.
116+ // If this node is installed in Node-RED 0.x, it will need to
117+ // fallback to using ` node.send `
118+ send = send || function() { node.send.apply(node,arguments) }
119+
120+ msg.payload = "hi";
121+ send(msg);
122+
123+ if (done) {
124+ done();
125+ }
126+ });
127+ {% endhighlight %}
128+
129+
63130If ` msg ` is null, no message is sent.
64131
65132If the node is sending a message in response to having received one, it should reuse
@@ -154,24 +221,8 @@ this.debug("Log something more details for debugging the node's behaviour");
154221
155222{% endhighlight %}
156223
157-
158224The ` warn ` and ` error ` messages also get sent to the flow editor debug tab.
159225
160- #### Handling errors
161-
162- If the node encounters an error that should halt the current flow, it should log
163- the event with the ` this.error ` function.
164-
165- If the error is one that a user of the node may want to handle for themselves,
166- the function should be called with the original message (or an empty message if
167- this is an Input node) as the second argument:
168-
169- {% highlight javascript %}
170- node.error("hit an error", msg);
171- {% endhighlight %}
172-
173- This will trigger any Catch nodes present on the same tab.
174-
175226### Setting status
176227
177228Whilst running, a node is able to share status information with the editor UI.
0 commit comments