MQTT is a widely used protocol in IoT space, with low overhead and targeted towards constrained devices. However they are not real time and full-duplex. and when that is needed WebSocket can be an alternative. Moreover WebSocket can take more payload (w.r.t Size) then MQTT. I will not get into that detail of WS or MQTT there are lot of articles available. This post is going to be about using WS to exchange data between ESP32 and Node-RED. WS does not need any intermediary like a broker or so and is a 1:1 communication protocol.
1. WebSocket with Node-RED:
Node-RED comes with Out of the Box support for WebSocket. You get WS in and Out nodes. In node is something that gives you incoming messages and out node, as you can assume, is used to write to the socket.
The nodes can either be used as server or client. In this case we are going to use it as server. Both the cases you need to configure the nodes. To know more about how to use WS in Node-RED checkout the following video.
2. Send data from ESP32 [Arduino] to Node-RED over WebSocket:
To use WebSocket in Arduino, I have used ArduinoWebsockets(http://bit.ly/3LeAGVl) library. Also to parse JSON objects I have used ArduinoJson(http://bit.ly/3ZHI5Rs) library. You can find the complete code here (https://github.com/WGLabz/Arduino-Node-RED-Websocket). As of now the code simulates a Temperature and humidity sensor and send random values through WebSocket every x seconds, which is configurable, actually you can changes the code to set it from node-RED and persist it in Arduino. It also receives messages sent by Node-RED and if the message which is a JSON string ({data: 1 | 0}) with a data key which can have value 1 or 0, has data key then it changes the on-board LED status accordingly.
3. Storing data to InfluxDB from Node-RED
For timeseries data InfluxDB is an appropriate DB to use. Its also free to use. Node-RED by default does not support InfluxDB OOB. You need to install required libraries for that. I have used this one https://flows.nodered.org/node/node-red-contrib-influxdb. The data received from ESP32 is written to the InfluxDB database and can be queried and displayed in tools like Grafana or Node-RED dashboard. Here i am just storing the randomly generated Temperature and Humidity data to InfluxDB.
I hope this help you in someway or other. You can find links to everything that I have used in the description of respective videos in YouTube. There are lot of links so am not posting here.