Node-RED Link ノードガイド

☁️ クラウドOK 環境とは?

目次

1. 概要

Link ノードは、フロー内やフロー間で「仮想的なワイヤー」を作成するノードです。物理的なワイヤーを引かずにノード同士を接続でき、複雑なフローを整理するのに役立ちます。

内線電話に例えると、Link ノードは会社の内線電話システムのようなものです。離れた部署(別のフロータブ)にいる人でも、内線番号(Link名)を使えば直接つながることができます。

Link ノードを使うメリット:

動作を図で見る

link ノードは、線を引かずに msg を別の場所へ渡します。link out に入った msg は、つながっている link in から出てきます。

フロー 1フロー 2msglink outlink inmsg見えない線でワープ
フローをまたいで、link out から link in へ msg が渡る

link call を使うと、共通の処理を呼び出して、結果を受け取れます。

msglink call結果の msg呼び出される処理(サブルーチン)link inchangelink out呼び出し元へ戻る行き戻り
呼び出した処理の最後の link out から、msg が link call へ戻ってくる

2. 3種類のLinkノード

📥 Link In

Link In

役割: メッセージの受信口

特徴:

  • Link Out または Link Call からメッセージを受け取る
  • 複数の Link Out から受信可能

📤 Link Out

Link Out

役割: メッセージの送信口

特徴:

  • Link In にメッセージを送信
  • 複数の Link In に同時送信可能

📞 Link Call

Link Call

役割: サブフローを呼び出して結果を受け取る

特徴:

  • Link In を呼び出し、Link Out(return)で結果を受け取る
  • 関数呼び出しのような動作

Link Out と Link Call の違い

特徴Link Out(通常モード)Link Call
動作送りっぱなし(Fire and Forget)結果を待って受け取る
戻り値なしあり(Link Out return経由)
用途通知、ログ記録、分岐共通処理の呼び出し
接続先Link InLink In(return用Link Out必須)

3. 設定詳細

Link In プロパティ

受信口の Link In で設定できる欄は名前だけです。名前の下には、このノードへつながる Link Out や Link Call を選ぶ一覧が並びます(この一覧にラベルはありません)。

設定項目説明既定値
名前このリンクの識別名。Link Out や Link Call の接続先一覧に、この名前が表示される(なし)

Link Out プロパティ

送信口の Link Out は、モードでノードの役割を切り替えます。モードの下に、接続先の Link In を選ぶ一覧が表示されます(この一覧にラベルはありません)。

設定項目説明既定値
モードノードの役割を選ぶ。選択肢は下表の2つ接続された全てのlinkノードへ送信
名前このリンクの識別名(なし)

モードの選択肢:

選択肢意味
接続された全てのlinkノードへ送信通常の送信口。選んだ Link In すべてへメッセージを送る(送りっぱなし)
link callノードへ返却Link Call から呼ばれた処理の戻り口にする。呼び出し元の Link Call へ結果を返す

Link Call プロパティ

Link Call は Link In を呼び出し、戻り口(「link callノードへ返却」モードの Link Out)からの結果を待ちます。「対象を固定で指定」のときは、リンクの種類の下に表示される一覧から呼び出す Link In を選びます。

設定項目説明既定値
タイムアウト呼び出し先からの応答を待つ上限。これを超えるとエラーになる30(秒)
リンクの種類呼び出し先の決め方を選ぶ。選択肢は下表の2つ対象を固定で指定
名前ノードの識別名(なし)

リンクの種類の選択肢:

選択肢意味
対象を固定で指定設定画面で呼び出す Link In を選んでおく
対象を動的に指定 (msg.target)受信したメッセージの msg.target で呼び出し先を切り替える

4. 実用的な使用パターン

サンプルフローのインポート方法:

  1. 下のフローJSON(公式サンプルフロー、または応用フロー)をコピー
  2. Node-REDエディタで メニュー → 読み込み を選択
  3. JSONをペーストして「読み込み」をクリック

公式サンプルフロー

最初のサンプルフローは、Node-RED本体に同梱されている公式サンプル3つ(「Link within a tab」「Link across tabs」「Link call」)をまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。1つ目と3つ目は「link」というタブに入れ、2つ目はタブをまたぐ例なので、公式のまま「TAB: 1st」「TAB: 2nd」の2つのタブに分けてあります。読み込むと、タブが3つ増えます。エディタの メニュー → 読み込み → サンプル → node-red → common → link からも、元のフローを開けます。

📥 公式サンプルフローJSON(クリックで展開)
[ { "id": "676976faf9e01206", "type": "tab", "label": "link", "disabled": false, "info": "", "env": [] }, { "id": "a30d20e6.ec6dc", "type": "link in", "z": "676976faf9e01206", "name": "", "links": [ "70d6f012.8fe6d" ], "x": 235, "y": 240, "wires": [ [ "6bf52c5c.d301c4" ] ] }, { "id": "70d6f012.8fe6d", "type": "link out", "z": "676976faf9e01206", "name": "", "links": [ "a30d20e6.ec6dc" ], "x": 315, "y": 180, "wires": [] }, { "id": "353c85ce.993d0a", "type": "inject", "z": "676976faf9e01206", "name": "例1: 実行", "topic": "", "payload": "Hello, World!", "payloadType": "str", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "x": 210, "y": 180, "wires": [ [ "70d6f012.8fe6d" ] ] }, { "id": "6bf52c5c.d301c4", "type": "debug", "z": "676976faf9e01206", "name": "例1: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 370, "y": 240, "wires": [] }, { "id": "62ea32aa.d73aac", "type": "comment", "z": "676976faf9e01206", "name": "Example: Link Node", "info": "Output of link out node can be connected to input of link in node. The connection between links in/out is not shown, so the flow representation can be simplified.", "x": 130, "y": 40, "wires": [] }, { "id": "85133fcc.e482", "type": "comment", "z": "676976faf9e01206", "name": "Link output of inject node to input of debug node", "info": "", "x": 260, "y": 100, "wires": [] }, { "id": "c588bc36.87fec", "type": "comment", "z": "676976faf9e01206", "name": "↓ connect to link in node", "info": "", "x": 410, "y": 140, "wires": [] }, { "id": "8abca900.6dfe78", "type": "comment", "z": "676976faf9e01206", "name": "↑ connect from link out node", "info": "", "x": 340, "y": 280, "wires": [] }, { "id": "0364d0b105293afc", "type": "comment", "z": "676976faf9e01206", "name": "Example: Link Call", "info": "### Link call node\nThe `link call` node can call a `link in` node then get result from `link out` node.\nIt can be set to a fixed target or passed a `target` in the `msg` for dynamic calling.\n\n### Function link call\nA `link in` node can be called from a function using `node.linkcall`\n", "x": 170, "y": 360, "wires": [] }, { "id": "f0e17fb6266e3e46", "type": "comment", "z": "676976faf9e01206", "name": "↓ call link (fixed target)", "info": "", "x": 420, "y": 420, "wires": [] }, { "id": "2869c5a483bad49f", "type": "inject", "z": "676976faf9e01206", "name": "例3a: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 200, "y": 460, "wires": [ [ "06a8cdd201630b4c" ] ] }, { "id": "943afa6102c26e24", "type": "debug", "z": "676976faf9e01206", "name": "例3a: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 610, "y": 460, "wires": [] }, { "id": "06a8cdd201630b4c", "type": "link call", "z": "676976faf9e01206", "name": "", "links": [ "e1b6b1fbd9b33c6d" ], "timeout": "30", "x": 390, "y": 460, "wires": [ [ "943afa6102c26e24" ] ] }, { "id": "b23d2de4047ac09b", "type": "link call", "z": "676976faf9e01206", "name": "", "links": [ "e1b6b1fbd9b33c6d" ], "linkType": "dynamic", "timeout": "30", "x": 380, "y": 580, "wires": [ [ "94c83d101ad168b0" ] ] }, { "id": "2706a2894820982a", "type": "inject", "z": "676976faf9e01206", "name": "例3b: 実行", "props": [ { "p": "payload" }, { "p": "target", "v": "subroutine-1", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 200, "y": 580, "wires": [ [ "b23d2de4047ac09b" ] ] }, { "id": "94c83d101ad168b0", "type": "debug", "z": "676976faf9e01206", "name": "例3b: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 610, "y": 580, "wires": [] }, { "id": "57355f9fbe74f23c", "type": "comment", "z": "676976faf9e01206", "name": "↓ set msg.target", "info": "", "x": 200, "y": 540, "wires": [] }, { "id": "558bf6ebcececd37", "type": "inject", "z": "676976faf9e01206", "name": "例3c: 実行", "props": [ { "p": "payload" }, { "p": "target", "v": "subroutine-1", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 200, "y": 700, "wires": [ [ "9095bcd27e7c3bd7" ] ] }, { "id": "4dbf26da9c203018", "type": "debug", "z": "676976faf9e01206", "name": "例3c: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 610, "y": 700, "wires": [] }, { "id": "2b032ec174258528", "type": "comment", "z": "676976faf9e01206", "name": "↓ call subroutine-1 from a function", "info": "", "x": 450, "y": 660, "wires": [] }, { "id": "9095bcd27e7c3bd7", "type": "function", "z": "676976faf9e01206", "name": "call subroutine-1", "func": "\nconst m = await node.linkcall('subroutine-1', msg, { timeout: 1000 })\n\nreturn m;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 400, "y": 700, "wires": [ [ "4dbf26da9c203018" ] ] }, { "id": "4237c6c2ee2e5c03", "type": "comment", "z": "676976faf9e01206", "name": "↓ call link in (target set by msg.target)", "info": "", "x": 470, "y": 540, "wires": [] }, { "id": "502424230fe03b8e", "type": "group", "z": "676976faf9e01206", "name": "subroutine-1", "style": { "label": true }, "nodes": [ "e1b6b1fbd9b33c6d", "547ff90d37be5bfa", "5dc4c8eee329ed86", "37c42be69a257586", "f2a44468d6af6018" ], "x": 134, "y": 799, "w": 532, "h": 122 }, { "id": "e1b6b1fbd9b33c6d", "type": "link in", "z": "676976faf9e01206", "g": "502424230fe03b8e", "name": "subroutine-1", "links": [], "x": 175, "y": 840, "wires": [ [ "547ff90d37be5bfa" ] ] }, { "id": "547ff90d37be5bfa", "type": "change", "z": "676976faf9e01206", "g": "502424230fe03b8e", "name": "", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "Hello, from subroutine-1", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 400, "y": 840, "wires": [ [ "5dc4c8eee329ed86" ] ] }, { "id": "5dc4c8eee329ed86", "type": "link out", "z": "676976faf9e01206", "g": "502424230fe03b8e", "name": "", "mode": "return", "links": [], "x": 625, "y": 840, "wires": [] }, { "id": "37c42be69a257586", "type": "comment", "z": "676976faf9e01206", "g": "502424230fe03b8e", "name": "↑ link node \"sunbroutine-1\"", "info": "", "x": 270, "y": 880, "wires": [] }, { "id": "f2a44468d6af6018", "type": "comment", "z": "676976faf9e01206", "g": "502424230fe03b8e", "name": "return to caller ↑ ", "info": "", "x": 560, "y": 880, "wires": [] }, { "id": "f51b8a1a.95b448", "type": "tab", "label": "TAB: 1st", "disabled": false, "info": "" }, { "id": "ec5fae59.42a3b", "type": "tab", "label": "TAB: 2nd", "disabled": false, "info": "" }, { "id": "fcd2b35a.6a7c4", "type": "link out", "z": "f51b8a1a.95b448", "name": "", "links": [ "f5fead9.12cdf5", "cc961da1.25402" ], "x": 335, "y": 200, "wires": [] }, { "id": "41a35965.1b4ed8", "type": "inject", "z": "f51b8a1a.95b448", "name": "例2: 実行", "topic": "", "payload": "Hello, World!", "payloadType": "str", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "x": 230, "y": 200, "wires": [ [ "fcd2b35a.6a7c4" ] ] }, { "id": "5b4e5338.81ce5c", "type": "comment", "z": "f51b8a1a.95b448", "name": "Example: Link Node across tabs", "info": "Output of link out node can be connected to input of link in node. The connection between links in/out is not shown, so the flow representation can be simplified.", "x": 190, "y": 60, "wires": [] }, { "id": "6cc2116d.b2d41", "type": "comment", "z": "f51b8a1a.95b448", "name": "Link output of inject node to input of debug node in 2nd tab", "info": "", "x": 320, "y": 120, "wires": [] }, { "id": "bdc2bf14.e00a6", "type": "comment", "z": "f51b8a1a.95b448", "name": "↓ connect to link in node in 2nd tab", "info": "", "x": 460, "y": 160, "wires": [] }, { "id": "cc961da1.25402", "type": "link in", "z": "ec5fae59.42a3b", "name": "", "links": [ "fcd2b35a.6a7c4" ], "x": 155, "y": 180, "wires": [ [ "ce8b8ea5.364cb" ] ] }, { "id": "ce8b8ea5.364cb", "type": "debug", "z": "ec5fae59.42a3b", "name": "例2: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 290, "y": 180, "wires": [] }, { "id": "64752acd.6cfcb4", "type": "comment", "z": "ec5fae59.42a3b", "name": "↑ connect from link out node in 1st tab", "info": "", "x": 290, "y": 220, "wires": [] }, { "id": "299b832b.6db7dc", "type": "comment", "z": "ec5fae59.42a3b", "name": "Example: Link Node across tabs", "info": "Output of link out node can be connected to input of link in node. The connection between links in/out is not shown, so the flow representation can be simplified.", "x": 190, "y": 60, "wires": [] }, { "id": "aa11903.1daf77", "type": "comment", "z": "ec5fae59.42a3b", "name": "Link output of inject node in 1st tab to input of debug node", "info": "", "x": 310, "y": 120, "wires": [] } ]

フローの読み解き

読み込むと、「link」「TAB: 1st」「TAB: 2nd」の3つのタブが増えます。例は全部で5つあり、どれも「例N: 実行」(inject)のボタンで動かし、同じ番号の「例N: 結果」(debug)に結果が出ます。link in・link out・link call のノードには名前が付いていないものが多いので、ここでは種類の名前で呼びます。

例 場所と、comment ノードの見出し(意味) ノードの並びと設定 ボタンを押すと出るもの
例1 タブ「link」の上半分。Link output of inject node to input of debug node(inject の出力を、link で debug の入力へつなぐ) 上の行:「例1: 実行」→ link out。下の行: link in →「例1: 結果」。link out の接続先に、下の行の link in を選んである。2つのノードの間に配線は無い。「↓ connect to link in node」「↑ connect from link out node」の comment が、つながりの向きを示している 「例1: 結果」に Hello, World!
例2 タブ「TAB: 1st」と「TAB: 2nd」。Example: Link Node across tabs(タブをまたぐ link ノード) 「TAB: 1st」に「例2: 実行」→ link out、「TAB: 2nd」に link in →「例2: 結果」がある。link out の接続先は、別のタブの link in 「TAB: 1st」でボタンを押すと、「TAB: 2nd」の「例2: 結果」に Hello, World!
例3a タブ「link」の下半分。↓ call link (fixed target)(link を呼び出す。呼び出し先は固定) 「例3a: 実行」→ link call →「例3a: 結果」。link call の「リンクの種類」は「対象を固定で指定」で、呼び出し先に「subroutine-1」(link in)を選んである 「例3a: 結果」に Hello, from subroutine-1
例3b ↓ set msg.target(msg.target を入れる)、↓ call link in (target set by msg.target)(msg.target で決めた link in を呼び出す) 「例3b: 実行」→ link call →「例3b: 結果」。inject が msg.target に、呼び出す link in の名前(subroutine-1)を入れて送る。link call の「リンクの種類」は「対象を動的に指定 (msg.target)」 「例3b: 結果」に Hello, from subroutine-1
例3c ↓ call subroutine-1 from a function(function から subroutine-1 を呼び出す) 「例3c: 実行」→「call subroutine-1」(function)→「例3c: 結果」。function のコードが、node.linkcall('subroutine-1', msg, { timeout: 1000 }) で link in を呼び出し、返ってきた msg を次へ送る 「例3c: 結果」に Hello, from subroutine-1

例3a〜3c は、呼び出し方が違うだけで、呼び出す相手は同じです。相手は、タブ「link」のいちばん下にある、グループ「subroutine-1」です。

呼び出される側 ノードの並びと設定
グループ「subroutine-1」 「subroutine-1」(link in)→ change → link out。change が、msg.payload を文字列の Hello, from subroutine-1 に書き換える。link out は「モード」が「link callノードへ返却」で、呼び出した側へ msg を返す。この link out には、接続先を選んでいない(呼び出した相手へ返すため)。comment の「↑ link node "sunbroutine-1"」は、公式サンプルのつづりのままで、subroutine-1 のことである

応用(サイト独自): 値を返す共通処理と、複数の送信先

ここから先は公式サンプルではなく、本サイトが用意した応用教材です。基本のつなぎ方、値を2倍にして返す共通処理の呼び出し、1つの link out から2つの link in へ送る部分があり、下のパターン1〜3の実例になっています。

📥 応用フローJSON(クリックで展開)
[ { "id": "link_sample_tab", "type": "tab", "label": "Link サンプル", "disabled": false, "info": "" }, { "id": "link_comment1", "type": "comment", "z": "link_sample_tab", "name": "━━━ 基本: Link Out → Link In ━━━", "info": "", "x": 180, "y": 40, "wires": [] }, { "id": "inject_basic", "type": "inject", "z": "link_sample_tab", "name": "送信", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "Hello via Link!", "payloadType": "str", "x": 130, "y": 100, "wires": [["link_out_basic"]] }, { "id": "link_out_basic", "type": "link out", "z": "link_sample_tab", "name": "送信口", "mode": "link", "links": ["link_in_basic"], "x": 295, "y": 100, "wires": [] }, { "id": "link_in_basic", "type": "link in", "z": "link_sample_tab", "name": "受信口", "links": ["link_out_basic"], "x": 435, "y": 100, "wires": [["debug_basic"]] }, { "id": "debug_basic", "type": "debug", "z": "link_sample_tab", "name": "受信確認", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 590, "y": 100, "wires": [] }, { "id": "link_comment2", "type": "comment", "z": "link_sample_tab", "name": "━━━ Link Call(サブルーチン呼び出し)━━━", "info": "", "x": 210, "y": 180, "wires": [] }, { "id": "inject_call", "type": "inject", "z": "link_sample_tab", "name": "数値入力", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "5", "payloadType": "num", "x": 140, "y": 240, "wires": [["link_call_double"]] }, { "id": "link_call_double", "type": "link call", "z": "link_sample_tab", "name": "2倍処理を呼び出し", "links": ["link_in_double"], "linkType": "static", "timeout": "30", "x": 350, "y": 240, "wires": [["debug_call_result"]] }, { "id": "debug_call_result", "type": "debug", "z": "link_sample_tab", "name": "結果: 10", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 560, "y": 240, "wires": [] }, { "id": "link_comment3", "type": "comment", "z": "link_sample_tab", "name": "【共通処理】2倍にする", "info": "", "x": 160, "y": 300, "wires": [] }, { "id": "link_in_double", "type": "link in", "z": "link_sample_tab", "name": "2倍処理入口", "links": [], "x": 155, "y": 360, "wires": [["func_double"]] }, { "id": "func_double", "type": "function", "z": "link_sample_tab", "name": "×2", "func": "msg.payload = msg.payload * 2;\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 310, "y": 360, "wires": [["link_out_return"]] }, { "id": "link_out_return", "type": "link out", "z": "link_sample_tab", "name": "結果を返す", "mode": "return", "links": [], "x": 455, "y": 360, "wires": [] }, { "id": "link_comment4", "type": "comment", "z": "link_sample_tab", "name": "━━━ 複数の送信先 ━━━", "info": "", "x": 160, "y": 440, "wires": [] }, { "id": "inject_multi", "type": "inject", "z": "link_sample_tab", "name": "イベント発生", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "{\"event\": \"button_click\", \"time\": \"2024-01-15\"}", "payloadType": "json", "x": 150, "y": 500, "wires": [["link_out_multi"]] }, { "id": "link_out_multi", "type": "link out", "z": "link_sample_tab", "name": "複数送信", "mode": "link", "links": ["link_in_log", "link_in_notify"], "x": 315, "y": 500, "wires": [] }, { "id": "link_in_log", "type": "link in", "z": "link_sample_tab", "name": "ログ処理", "links": ["link_out_multi"], "x": 445, "y": 480, "wires": [["debug_log"]] }, { "id": "debug_log", "type": "debug", "z": "link_sample_tab", "name": "📝 ログ記録", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 590, "y": 480, "wires": [] }, { "id": "link_in_notify", "type": "link in", "z": "link_sample_tab", "name": "通知処理", "links": ["link_out_multi"], "x": 445, "y": 520, "wires": [["debug_notify"]] }, { "id": "debug_notify", "type": "debug", "z": "link_sample_tab", "name": "🔔 通知送信", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 590, "y": 520, "wires": [] } ]

フローの読み解き

応用フローは、3つの部分に分かれています。各部分の先頭にある comment ノードの名前が、下の表の「部分」に当たります。

部分 ノードの並び 設定と、ボタンを押すと出るもの パターン
基本: Link Out → Link In 「送信」(inject)→「送信口」(link out)、「受信口」(link in)→「受信確認」(debug) 「送信口」の接続先は「受信口」。「送信」を押すと、「受信確認」に Hello via Link! が出る。公式サンプルの例1と同じ作りで、ノードに名前を付けてある パターン1
Link Call(サブルーチン呼び出し)と、その下の【共通処理】2倍にする 呼び出す側:「数値入力」(inject)→「2倍処理を呼び出し」(link call)→「結果: 10」(debug)。呼び出される側:「2倍処理入口」(link in)→「×2」(function)→「結果を返す」(link out) link call の呼び出し先は「2倍処理入口」。「×2」が msg.payload を2倍にし、「結果を返す」(「モード」は「link callノードへ返却」)が呼び出した側へ返す。「数値入力」(5 を送る)を押すと、「結果: 10」に 10 が出る。公式サンプルの例3a と違い、渡した値を使って計算した結果が返る パターン2
複数の送信先 「イベント発生」(inject)→「複数送信」(link out)、「ログ処理」(link in)→「📝 ログ記録」(debug)、「通知処理」(link in)→「🔔 通知送信」(debug) 「複数送信」の接続先に、「ログ処理」と「通知処理」の2つを選んである。「イベント発生」を押すと、「📝 ログ記録」と「🔔 通知送信」の両方に、同じ {"event":"button_click","time":"2024-01-15"} が出る パターン3

パターン4(共通検証処理の再利用)のフローは、公式サンプルにも応用フローにも含まれていません。同じ共通処理を2か所から呼び出す形は、演習4で作ります。

パターン1: 基本的な Link Out → Link In

応用フローの「基本: Link Out → Link In」を参照してください。

用途: ワイヤーを交差させず離れた場所へメッセージを送信

Inject → Link Out - - - Link In → Debug

ポイント: ワイヤーの交差を避けたり、離れた場所にメッセージを送る

パターン2: Link Call(サブルーチン呼び出し)

応用フローの「Link Call(サブルーチン呼び出し)」を参照してください。

用途: 共通処理を呼び出して結果を取得

Inject → Link Call → 結果
【呼び出し先】 2倍処理入口 → ×2 → 結果を返す

ポイント:

パターン3: 複数の送信先

応用フローの「複数の送信先」を参照してください。

用途: 1つの送信元から複数の受信先へ同時に送信

Link Out - - -受信A - - -受信B

ポイント: 1つの Link Out から複数の Link In に送信可能

パターン4: 共通検証処理の再利用

このパターンのフローは、サンプルフローには含まれていません。

用途: 同じ検証処理を複数箇所から呼び出して再利用

テストA → 検証呼出 → 結果
テストB → 検証呼出 → 結果

ポイント: 同じ検証ロジックを複数箇所から呼び出して再利用

5. 演習問題

演習1: 基本的なLink接続 初級

課題:Link Out と Link In を使って、ワイヤーを引かずにメッセージを送信するフローを作成してください。

💼 使いどころ: ワイヤーの交差を避けて離れた場所へメッセージを送りたい場面で使います。

成功の条件:

💡 ヒント

1. Link Out ノードの設定で、接続先の Link In を選択します

2. 名前を分かりやすくつけると管理しやすくなります

✅ 解答例フロー
[ {"id": "ex1_tab", "type": "tab", "label": "演習1"}, {"id": "ex1_inject", "type": "inject", "z": "ex1_tab", "name": "送信", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "Link経由のメッセージ", "payloadType": "str", "x": 130, "y": 100, "wires": [["ex1_link_out"]]}, {"id": "ex1_link_out", "type": "link out", "z": "ex1_tab", "name": "送信口", "mode": "link", "links": ["ex1_link_in"], "x": 275, "y": 100, "wires": []}, {"id": "ex1_link_in", "type": "link in", "z": "ex1_tab", "name": "受信口", "links": ["ex1_link_out"], "x": 435, "y": 100, "wires": [["ex1_debug"]]}, {"id": "ex1_debug", "type": "debug", "z": "ex1_tab", "name": "確認", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 570, "y": 100, "wires": []} ]

演習2: 複数の受信先 初級

課題:1つの Link Out から2つの Link In にメッセージを送信するフローを作成してください。

💼 使いどころ: 1つのメッセージを複数の処理へ同時に振り分けたい場面で使います。

成功の条件:

💡 ヒント

Link Out ノードの設定で、複数の Link In を選択できます。

✅ 解答例フロー
[ {"id": "ex2_tab", "type": "tab", "label": "演習2"}, {"id": "ex2_inject", "type": "inject", "z": "ex2_tab", "name": "イベント", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "共通データ", "payloadType": "str", "x": 140, "y": 140, "wires": [["ex2_link_out"]]}, {"id": "ex2_link_out", "type": "link out", "z": "ex2_tab", "name": "分岐送信", "mode": "link", "links": ["ex2_link_in1", "ex2_link_in2"], "x": 295, "y": 140, "wires": []}, {"id": "ex2_link_in1", "type": "link in", "z": "ex2_tab", "name": "処理A", "links": ["ex2_link_out"], "x": 435, "y": 100, "wires": [["ex2_debug1"]]}, {"id": "ex2_debug1", "type": "debug", "z": "ex2_tab", "name": "処理A実行", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 590, "y": 100, "wires": []}, {"id": "ex2_link_in2", "type": "link in", "z": "ex2_tab", "name": "処理B", "links": ["ex2_link_out"], "x": 435, "y": 180, "wires": [["ex2_debug2"]]}, {"id": "ex2_debug2", "type": "debug", "z": "ex2_tab", "name": "処理B実行", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 590, "y": 180, "wires": []} ]

演習3: Link Call の基本 中級

課題:Link Call を使って「入力値を3倍にする」共通処理を作成し、呼び出すフローを作ってください。

💼 使いどころ: 共通の計算処理を呼び出して、その場で結果を受け取りたい場面で使います。

成功の条件:

💡 ヒント

1. 共通処理: Link In → Change(×3。「値の代入」で msg.payload に「JSONata式」型の payload * 3) → Link Out(返却モード)

2. 呼び出し側: Inject → Link Call → Debug

3. Link Out のモードを「link callノードへ返却」に設定することが重要です

✅ 解答例フロー
[ {"id": "ex3_tab", "type": "tab", "label": "演習3"}, {"id": "ex3_comment", "type": "comment", "z": "ex3_tab", "name": "━━━ 呼び出し側 ━━━", "info": "", "x": 140, "y": 40, "wires": []}, {"id": "ex3_inject", "type": "inject", "z": "ex3_tab", "name": "7を入力", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "7", "payloadType": "num", "x": 140, "y": 100, "wires": [["ex3_call"]]}, {"id": "ex3_call", "type": "link call", "z": "ex3_tab", "name": "3倍処理呼び出し", "links": ["ex3_link_in"], "linkType": "static", "timeout": "30", "x": 340, "y": 100, "wires": [["ex3_debug"]]}, {"id": "ex3_debug", "type": "debug", "z": "ex3_tab", "name": "結果: 21", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 530, "y": 100, "wires": []}, {"id": "ex3_comment2", "type": "comment", "z": "ex3_tab", "name": "━━━ 共通処理(3倍) ━━━", "info": "", "x": 160, "y": 180, "wires": []}, {"id": "ex3_link_in", "type": "link in", "z": "ex3_tab", "name": "3倍処理", "links": [], "x": 135, "y": 240, "wires": [["ex3_change"]]}, {"id": "ex3_change", "type": "change", "z": "ex3_tab", "name": "×3", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "payload * 3", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 290, "y": 240, "wires": [["ex3_return"]]}, {"id": "ex3_return", "type": "link out", "z": "ex3_tab", "name": "結果を返す", "mode": "return", "links": [], "x": 435, "y": 240, "wires": []} ]

演習4: 再利用可能な検証処理 上級

課題:「入力値が正の数かどうかを検証する」共通処理を Link Call で作成し、複数箇所から呼び出せるようにしてください。

💼 使いどころ: 複数箇所から同じ検証処理を呼び出し、ロジックを1箇所にまとめたい場面で使います。

要求仕様:

成功の条件:

💡 ヒント

Switch ノードで条件分岐を行い、出力ごとに Change ノードで結果を msg に設定して返します。

  • Switch: プロパティ msg.payload、ルール1「>」(数値 0)、ルール2「その他」
  • 1つ目の出力の Change: 「値の代入」で msg.valid に「真偽」の true
  • 2つ目の出力の Change: 「値の代入」で msg.valid に「真偽」の false、msg.error に文字列「正の数を入力してください」
  • 2つの Change は、どちらも同じ Link Out(「link callノードへ返却」)につなぐ
✅ 解答例フロー
[ {"id": "ex4_tab", "type": "tab", "label": "演習4"}, {"id": "ex4_comment1", "type": "comment", "z": "ex4_tab", "name": "━━━ テスト呼び出し ━━━", "info": "", "x": 150, "y": 40, "wires": []}, {"id": "ex4_inject1", "type": "inject", "z": "ex4_tab", "name": "正の数: 10", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "10", "payloadType": "num", "x": 150, "y": 100, "wires": [["ex4_call"]]}, {"id": "ex4_inject2", "type": "inject", "z": "ex4_tab", "name": "負の数: -5", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "-5", "payloadType": "num", "x": 150, "y": 140, "wires": [["ex4_call"]]}, {"id": "ex4_call", "type": "link call", "z": "ex4_tab", "name": "正の数チェック", "links": ["ex4_validate_in"], "linkType": "static", "timeout": "30", "x": 360, "y": 120, "wires": [["ex4_debug"]]}, {"id": "ex4_debug", "type": "debug", "z": "ex4_tab", "name": "検証結果", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "true", "targetType": "full", "statusVal": "valid", "statusType": "msg", "x": 550, "y": 120, "wires": []}, {"id": "ex4_comment2", "type": "comment", "z": "ex4_tab", "name": "━━━ 検証処理(共通) ━━━", "info": "", "x": 160, "y": 220, "wires": []}, {"id": "ex4_validate_in", "type": "link in", "z": "ex4_tab", "name": "正の数検証", "links": [], "x": 145, "y": 280, "wires": [["ex4_switch"]]}, {"id": "ex4_switch", "type": "switch", "z": "ex4_tab", "name": "正の数?", "property": "payload", "propertyType": "msg", "rules": [{"t": "gt", "v": "0", "vt": "num"}, {"t": "else"}], "checkall": "true", "repair": false, "outputs": 2, "x": 310, "y": 280, "wires": [["ex4_valid"], ["ex4_invalid"]]}, {"id": "ex4_valid", "type": "change", "z": "ex4_tab", "name": "valid = true", "rules": [{"t": "set", "p": "valid", "pt": "msg", "to": "true", "tot": "bool"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 490, "y": 250, "wires": [["ex4_return"]]}, {"id": "ex4_invalid", "type": "change", "z": "ex4_tab", "name": "valid = false+エラー", "rules": [{"t": "set", "p": "valid", "pt": "msg", "to": "false", "tot": "bool"}, {"t": "set", "p": "error", "pt": "msg", "to": "正の数を入力してください", "tot": "str"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 510, "y": 310, "wires": [["ex4_return"]]}, {"id": "ex4_return", "type": "link out", "z": "ex4_tab", "name": "結果を返す", "mode": "return", "links": [], "x": 730, "y": 280, "wires": []} ]

6. まとめ

重要ポイント:

注意事項:

7. 実務活用例

ケース1: ログ記録の共通化

各処理からログ記録用の Link In にメッセージを送信し、ログ処理を一元管理。

ケース2: エラーハンドリング

エラー発生時に共通のエラー処理フローへ Link で転送。

ケース3: 機能別のタブ分割

入力処理、ビジネスロジック、出力処理をタブで分けて Link で連携。

ケース4: 共通バリデーション

Link Call で入力検証を呼び出し、結果に応じて処理を分岐。

📚 Node-RED 公式ドキュメント

🏠