Node Red client for Moleculer Microservices Framework with events, emit, call, request and response support.
- Now Supports v0.14 of Moleculer.
- Added Moleculer Instance to get moleculer client inside a function node.
On ~/.node-red folder:
npm install node-red-contrib-moleculer
npm install ioredis
or npm install nats
// to activate transporter config.
Please test and make feedback.
Example (NATS running on localhost:4222):
[{"id":"ff5015ff.932928","type":"moleculer-request-action","z":"e2032578.548eb8","broker":"15fdc347.1238cd","service":"39ab2b37.fd8374","name":"Math Add","topic":"add","params":"{}","paramsType":"json","x":300,"y":80,"wires":[["6d300aff.e17c34"]]},{"id":"edc74057.5ca1d","type":"moleculer-response-action","z":"e2032578.548eb8","name":"","x":780,"y":120,"wires":[]},{"id":"6d300aff.e17c34","type":"function","z":"e2032578.548eb8","name":"Function ADD","func":"let response = Number(msg.payload.a) + Number(msg.payload.b);\nmsg.payload = response;\nnode.send([null,null,msg]);\nmsg.emit('test.emit',\"OK\");\nmsg.broadcast('test.bcast',\"OK\")\n\nmsg.call('math.sub',{a:9,b:2},{meta:{y:33}}).then((data)=>{\n msg.payload = data\n node.send([null,msg])\n}).catch((err)=>{\n node.error(err, msg)\n})\n\n\nmsg.call('$none.actions').then((data)=>{\n msg.payload = data\n node.send([null,msg])\n}).catch((err)=>{\n node.error(err, msg)\n})\n\nreturn [msg]; // or node.send([msg])","outputs":2,"noerr":0,"initialize":"","finalize":"","x":520,"y":80,"wires":[["edc74057.5ca1d","1c9a2ec0.34da71"],["48eabdfc.c93f54"]]},{"id":"70a1b10.f31965","type":"moleculer-call","z":"e2032578.548eb8","broker":"15fdc347.1238cd","name":"","topic":"math.add","options":"{}","optionsType":"json","x":540,"y":320,"wires":[["8f89dcb1.f9e4d"]]},{"id":"d0471b75.2a3818","type":"inject","z":"e2032578.548eb8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"options","v":"{\"meta\":{\"z\":1}}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"a\":2,\"b\":3}","payloadType":"json","x":310,"y":320,"wires":[["70a1b10.f31965"]]},{"id":"8f89dcb1.f9e4d","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":320,"wires":[]},{"id":"c8d19b2e.7614b8","type":"moleculer-event","z":"e2032578.548eb8","broker":"15fdc347.1238cd","service":"39ab2b37.fd8374","name":"","topic":"test.emit","group":"","x":300,"y":200,"wires":[["bd3bfb1a.e21a98"]]},{"id":"f8a5d380.02c92","type":"moleculer-event","z":"e2032578.548eb8","broker":"15fdc347.1238cd","service":"39ab2b37.fd8374","name":"","topic":"test.bcast","group":"","x":300,"y":240,"wires":[["bd8089d.802c278"]]},{"id":"bd3bfb1a.e21a98","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":200,"wires":[]},{"id":"bd8089d.802c278","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":240,"wires":[]},{"id":"1c9a2ec0.34da71","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":80,"wires":[]},{"id":"287a0cf8.ecf0f4","type":"catch","z":"e2032578.548eb8","name":"Catch Call Error","scope":["6d300aff.e17c34","b0380e23.e0516"],"uncaught":false,"x":520,"y":40,"wires":[["a55bb923.d1a678"]]},{"id":"a55bb923.d1a678","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":40,"wires":[]},{"id":"bad82870.38dde8","type":"comment","z":"e2032578.548eb8","name":"Events","info":"","x":290,"y":160,"wires":[]},{"id":"beb82942.b8b798","type":"comment","z":"e2032578.548eb8","name":"Actions","info":"","x":290,"y":40,"wires":[]},{"id":"3a61a67c.c8e8ea","type":"comment","z":"e2032578.548eb8","name":"Calling Action","info":"","x":310,"y":280,"wires":[]},{"id":"9b91bc3e.f551","type":"moleculer-request-action","z":"e2032578.548eb8","broker":"15fdc347.1238cd","service":"39ab2b37.fd8374","name":"Math Sub","topic":"sub","params":"{\"a\":{\"type\":\"number\",\"positive\":true,\"integer\":true,\"convert\":true},\"b\":{\"type\":\"number\",\"positive\":true,\"integer\":true,\"convert\":true}}","paramsType":"json","x":300,"y":120,"wires":[["b0380e23.e0516"]]},{"id":"b0380e23.e0516","type":"function","z":"e2032578.548eb8","name":"Function SUB","func":"let response = Number(msg.payload.a) - Number(msg.payload.b);\nmsg.payload = response;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":140,"wires":[["6e612a8c.22b9a4","b63bc0d1.0edad"]]},{"id":"6e612a8c.22b9a4","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":240,"wires":[]},{"id":"b63bc0d1.0edad","type":"moleculer-response-action","z":"e2032578.548eb8","name":"","x":780,"y":200,"wires":[]},{"id":"9b7aee86.a556c","type":"moleculer-apigw","z":"e2032578.548eb8","broker":"15fdc347.1238cd","service":"3932022b.90c6ee","name":"","x":290,"y":520,"wires":[]},{"id":"7cb432a4.6d60ac","type":"moleculer-call","z":"e2032578.548eb8","broker":"15fdc347.1238cd","name":"","topic":"math.sub","options":"{}","optionsType":"json","x":540,"y":360,"wires":[["111e3b48.883b95"]]},{"id":"ce66fc53.82401","type":"inject","z":"e2032578.548eb8","name":"Wrong Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"a\":\"wrong data\",\"b\":3}","payloadType":"json","x":310,"y":440,"wires":[["7cb432a4.6d60ac"]]},{"id":"111e3b48.883b95","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":360,"wires":[]},{"id":"17907b92.ba15f4","type":"catch","z":"e2032578.548eb8","name":"","scope":["7cb432a4.6d60ac"],"uncaught":false,"x":550,"y":420,"wires":[["24b6067f.33165a"]]},{"id":"c0d9615.fb9d6a","type":"inject","z":"e2032578.548eb8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"a\":6,\"b\":3}","payloadType":"json","x":310,"y":360,"wires":[["7cb432a4.6d60ac"]]},{"id":"24b6067f.33165a","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":400,"wires":[]},{"id":"7c0238a9.f776e8","type":"inject","z":"e2032578.548eb8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"a\":\"5\",\"b\":3}","payloadType":"json","x":310,"y":400,"wires":[["7cb432a4.6d60ac"]]},{"id":"8e44ebae.53ad88","type":"comment","z":"e2032578.548eb8","name":"Call http://localhost:1880/$node/actions","info":"","x":390,"y":480,"wires":[]},{"id":"48eabdfc.c93f54","type":"debug","z":"e2032578.548eb8","name":"Msg Call inside function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":830,"y":160,"wires":[]},{"id":"bfd19cf0.c30ed","type":"moleculer-instance","z":"e2032578.548eb8","broker":"15fdc347.1238cd","name":"","topic":"mol","location":"flow","x":290,"y":600,"wires":[]},{"id":"66489625.146c08","type":"inject","z":"e2032578.548eb8","name":"Call inside Function","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":490,"y":600,"wires":[["6fbd596b.685bc8"]]},{"id":"6fbd596b.685bc8","type":"function","z":"e2032578.548eb8","name":"","func":"const mol = flow.get('mol');\n\nmol.call('math.sub',{a:9,b:2},{meta:{y:33}}).then((data)=>{\n msg.payload = data\n node.send(msg)\n}).catch((err)=>{\n node.error(err, msg)\n});","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":600,"wires":[["12db80bd.51662f"]]},{"id":"12db80bd.51662f","type":"debug","z":"e2032578.548eb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":830,"y":600,"wires":[]},{"id":"d7b9f8d.9996708","type":"comment","z":"e2032578.548eb8","name":"Using Moleculer Instance","info":"","x":350,"y":560,"wires":[]},{"id":"15fdc347.1238cd","type":"moleculer-config","z":"","name":"NATS","options":"{\"transporter\":\"nats://localhost:4222\"}","optionsType":"json"},{"id":"39ab2b37.fd8374","type":"moleculer-service-config","z":"","name":"math","version":"","settings":"{}"},{"id":"3932022b.90c6ee","type":"moleculer-service-config","z":"","name":"apigw","version":"","settings":"{}"}]