How to send mqtt to html response

Hello
Listening for mqtt on node-red dashboards.
I want to send mqtt data directly to the specified http address.
Is there a way to send without request?

What do you mean "without request"? If you want to send data to an HTTP endpoint, you have to send a request to that endpoint using the HTTP Request node.

Alternatively, this recipe shows how you can include data data capture in one flow in the response to an incoming HTTP request - Include data captured in another flow : Node-RED

image

So far, I have requested from the html page to the node-red server and received and processed the response.
I want to send data directly from node-red to html address without any request from html.
For example

image

I would like to do this

Use the HTTP Request node

https://cookbook.nodered.org/#http-requests

If you sending data that will probably need POST mode in the http request. That will depend on what the remote server requires of course.

There are several ways to do this.

As Nick has said, the http-in/out nodes come as a pair and you have to have a user connect to the url which sends typically a GET command to the http-in node. Web pages are user driven not server driven.

So to do what you want. You either have to have the latest MQTT data already in Node-RED so that you can send it as part of the response (http-out) (1). Or you have to create some kind of persistent connection between the users browser tab and the Node-RED server (2).

  1. You could combine your two flow examples so that both the http-in and mqtt-in nodes both go into the same function node. In that function node, you would have a test on the msg.topic and if it matches the MQTT subscription, you simply grab the data and save it to a context variable. Otherwise, you grab the latest context variable data previously saved and feed that into the response.

  2. In order to give the server a chance to send a later response - for example, after the initial page load completes, you can use websockets. This is how the Dashboard works. uibuilder will also do it. You could also create your own websocket using the websocket nodes.

Here is an example using uibuilder:


The full code for that is in the Node-RED flows site.

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