This gist describes an MQTT client writing sensor data to an Server Sent Events (SSE) end-point.
git clone https://gist.github.com/chainhead/715e48a2f157e2bfe3f012f587f8bf5b
npm i express mqtt
node server.jsIn a terminal, run either of these commands.
git clone https://gist.github.com/chainhead/715e48a2f157e2bfe3f012f587f8bf5b
npm i express mqtt
node client.jscurl -N http://localhost:3000/eventsYou can also see the events in a browser by pointing the browser to http://localhost:3000/events. However, please also the notes on browser support in server.js below.
The terminal will show the MQTT messages published from another terminal. See below.
In a separate terminal, publish MQTT messages to test.mosquitto.org on port 1883. The following code snippet shows the usage of mqtt command as the client. It can be installed with npm i -g mqtt.
mqtt publish -h test.mosquitto.org -t mqtt/sse/demo -m '{"a":"A"}'The server.js will receive the MQTT payload and publish a SSE for the same.