# PYNODE RED async message responses

**URL:** <https://discourse.nodered.org/t/pynode-red-async-message-responses/30409>\
**Category:** General\
**Created:** [20 July 2020 16:55 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409 "2020-07-20T16:55:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanajira](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shanajira/32/26105_2.png) [@shanajira](https://discourse.nodered.org/u/shanajira)\
**Post date:** [20 July 2020 16:55 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/1 "2020-07-20T16:55:13Z")

</div>

hello  
I am using successfully pynodered (Python node red) to do a lot of processing in the background for all my node-red needs.  
Now I am trying to add "async" asynchronous functionality to my backend system via pynodered.

in general, node.send via NODERED works for asynchronous message responses.  
But i dont think pynodered supports this.

has anyone tried sneding asynchronous message responses back to node-red from Python?  
any examples / code references will be appreciated.

thanks

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 July 2020 17:09 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/2 "2020-07-20T17:09:48Z")

</div>

> [@shanajira](#):
>
> pynodered

Looks to me as though this creates a simple TCP server (or maybe UDB or UNIX Pipes, I've not read the code). Your Python function is exported to update some JavaScript files that provide an automatic link. That's why you have to restart both your Python code and Node-RED if you make changes.

Fun but a little over-complex perhaps since you could actually use a single Python server with a single connection to a single node and get much of the same effect. But of course, that would require more programming knowledge from the users. Personally, I think I would have created a second channel in the Python server that output a list of available functions and then the node in Node-RED could request that list when you open the configuration UI in the Editor - that would save Node-RED from having to be reloaded each time.

But I digress from your question.

I see that Flask is being used for the Python server which is good since that is non-blocking and async I believe (my Python is a bit rusty).

> [@shanajira](#):
>
> has anyone tried sneding asynchronous message responses back to node-red from Python?  
> any examples / code references will be appreciated.

However, I cannot see any mechanism on the Python side that lets the server await a response from Node-RED. You would need this for what you've asked for.

Fundamentally, Python and Node-RED (being a node.js application) are very different in handling async processing. Node.js is very much async driven and Python is not (without 3rd-party libraries).

So unless you can program some way, on the Python side, to wait for a response (along with all the checks you need to prevent your server from being blocked), I think you are rather stuck.

* * *

Hmm, one rather annoying thing about the pynodered code is that they don't allow issues to be raised in GitHub so it is rather difficult to feed back any suggestions for improvements.

If your coding skills are up to it, I would create my own Python server that acts as an API server. You can then interact with it as a REST API using the core request node in Node-RED. That will let you GET data from your Python server and POST data to it for example.

---

<div class="post-metadata">

**Author:** ![shanajira](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shanajira/32/26105_2.png) [@shanajira](https://discourse.nodered.org/u/shanajira)\
**Post date:** [20 July 2020 17:43 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/3 "2020-07-20T17:43:18Z")

</div>

thank you @TotallyInformation - appreciate the detailed response.  
I think I can brush up my coding skills.  
So PynodeRED does not do what I need.  
NODE RED itself allows sending asynchronous messages via node.send followed by node.done  
This is what I wanted to acheive.

Regarding your comment:

> Fun but a little over-complex perhaps since you could actually use a single Python server with a single connection to a single node and get much of the same effect.

Can you elaborate how would I go about implementing the python modules, should I choose to accept this mission? 🙂  
in my python code, all I want is an ability talk to node-red server running on my localhost 1880.

Currently via PynodeRED I can grab the entire msg object, and node handle. (which I incorrectly thought was going to help me do node.send and node.done) but this so called "node" object from pynodered is not the one I can call these underlying core functions.

TIA  
shan

---

<div class="post-metadata">

**Author:** ![shanajira](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shanajira/32/26105_2.png) [@shanajira](https://discourse.nodered.org/u/shanajira)\
**Post date:** [20 July 2020 17:50 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/4 "2020-07-20T17:50:30Z")

</div>

I meant to ask for what will the top level flow in my python look like? not actual code. 🙂  
Also, can Python module (which I have written for normal socket programming) communicate to node-red server on local host :1880  
If so, where is the messaging protocol defined for NodeRED?  
thanks

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [20 July 2020 18:28 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/5 "2020-07-20T18:28:26Z")

</div>

Would it not be smart to use MQTT for the Python Server \<---\> NR communication.

Fast, reliable, easy on both ends. NR has already built in support for MQTT. Adding MQTT to any Python code is dead simple too

---

<div class="post-metadata">

**Author:** ![shanajira](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shanajira/32/26105_2.png) [@shanajira](https://discourse.nodered.org/u/shanajira)\
**Post date:** [20 July 2020 18:47 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/6 "2020-07-20T18:47:14Z")

</div>

thanks. I have added MQTT /implemented RabbitMQ completely for the project.  
I can reuse that for Node RED too.

My question is:  
-from my Python code, if I send MQTT commands/packets, will Node RED accept it? What format do I need to send them?  
Where is the detailed documentation for MQTT support of node red?

appreciate the discussion.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 July 2020 20:09 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/7 "2020-07-20T20:09:54Z")

</div>

> [@shanajira](#):
>
> Can you elaborate how would I go about implementing the python modules, should I choose to accept this mission?

Two parts.

1. Python server

2. Node-RED Node

* * *

I should add that I had been considering doing something like this myself as I needed to do some data analysis on some largish CSV files recently and this is an area where Python really excels. I ran out of time and had to use a nasty combination of Excel and bits of stuff written in a Jupyter notebook as a one-off instead of something repeatable.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 July 2020 20:19 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/8 "2020-07-20T20:19:52Z")

</div>

> [@shanajira](#):
>
> I meant to ask for what will the top level flow in my python look like? not actual code

The top level would be a simple Python web server. Probably with a big switch statement that called out to other modules as you need to.

> [@shanajira](#):
>
> Also, can Python module (which I have written for normal socket programming) communicate to node-red server on local host :1880

Well, if you already have something with either websockets or Unix pipes, then you are already mostly there. NR excels at websockets. Use the websocket-in/-out nodes.

> [@shanajira](#):
>
> where is the messaging protocol defined for NodeRED

You are over-thinking it. Just exchange serialised JSON and you are good to go with pretty much any data you want (well, anything that can be serialised JSON anyway!).

> [@shanajira](#):
>
> I have added MQTT /implemented RabbitMQ completely for the project.  
> I can reuse that for Node RED too.

Yes, you can use that instead. But a direct connection is likely more efficient in general. Just depends what you want to do and how fast you need it to happen. Also depends on what networking might impose on you. MQTT is useful for an extended network but for a simple local network, websockets will be more efficient.

A REST server is not as dynamic but translates well and is easily secured no matter what network you may have, including the Internet.

> [@shanajira](#):
>
> from my Python code, if I send MQTT commands/packets, will Node RED accept it? What format do I need to send them?

Again, no need to over-think things. Node-RED will handle anything you want to exchange. From simple text/numeric/boolean values to serialised JSON. Or anything else you like really as long as you can encode it on one side and decode on the other.

> [@shanajira](#):
>
> Where is the detailed documentation for MQTT support of node red?

In the documentation for the mqtt-in/-out nodes. Mostly, it "just works".

* * *

If you do use MQTT, the principles are the same as I've already described. Set up a Python server that lets you publish and subscribe from your own code modules/classes and keeps running in the background. That is if you need 2-way comms. e.g. you want to drive from Node-RED.

I don't know enough about MQTT libraries for Python to be able to say whether that will be especially effective. You will really still want to be able to implement an async service.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [20 July 2020 20:22 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/9 "2020-07-20T20:22:22Z")

</div>

> [@TotallyInformation](#):
>
> I see that Flask is being used for the Python server which is good since that is non-blocking and async I believe (my Python is a bit rusty).

Flask is very, very sync. I love it, but it’s sync and blocking. If I need async I use Quart, a framework inspired by Flask and API compatible as much as possible, but async.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 July 2020 20:24 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/10 "2020-07-20T20:24:40Z")

</div>

> [@afelix](#):
>
> Flask is very, very sync. I love it, but it’s sync and blocking. If I need async I use Quart, a framework inspired by Flask and API compatible as much as possible, but async.

Ah, thanks Lena, I didn't know that. Long time since I did much serious Python.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 August 2020 20:24 UTC](https://discourse.nodered.org/t/pynode-red-async-message-responses/30409/11 "2020-08-03T20:24:47Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
