内線電話に例えると、Link ノードは会社の内線電話システムのようなものです。離れた部署(別のフロータブ)にいる人でも、内線番号(Link名)を使えば直接つながることができます。
受信口の Link In で設定できる欄は名前だけです。名前の下には、このノードへつながる Link Out や Link Call を選ぶ一覧が並びます(この一覧にラベルはありません)。
送信口の Link Out は、モードでノードの役割を切り替えます。モードの下に、接続先の Link In を選ぶ一覧が表示されます(この一覧にラベルはありません)。
Link Call は Link In を呼び出し、戻り口(「link callノードへ返却」モードの Link Out)からの結果を待ちます。「対象を固定で指定」のときは、リンクの種類の下に表示される一覧から呼び出す Link In を選びます。
最初のサンプルフローは、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 のノードには名前が付いていないものが多いので、ここでは種類の名前で呼びます。
📥 応用フロー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": []
}
]
演習4: 再利用可能な検証処理 上級
課題:「入力値が正の数かどうかを検証する」共通処理を Link Call で作成し、複数箇所から呼び出せるようにしてください。
💼 使いどころ: 複数箇所から同じ検証処理を呼び出し、ロジックを1箇所にまとめたい場面で使います。
要求仕様:
- 正の数: msg.valid = true, msg.payload = 元の値
- 負の数または0: msg.valid = false, msg.error = "正の数を入力してください"
成功の条件:
- 「正の数: 10」ボタンをクリックすると、デバッグサイドバーに
msg.valid = true、msg.payload = 10 を含む結果が表示される
- 「負の数: -5」ボタンをクリックすると、
msg.valid = false、msg.error = "正の数を入力してください" を含む結果が表示される
- 両方のInjectボタンが同じLink Call(同じ検証共通処理)を呼び出しており、検証ロジックが1箇所に集約されている
- Debugノードのステータス欄に
msg.valid の値(true / false)が表示される
💡 ヒント
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": []}
]