Skip to content

Latest commit

 

History

History
75 lines (54 loc) · 1.21 KB

README.md

File metadata and controls

75 lines (54 loc) · 1.21 KB

MQTT to PostgreSQL demo in Node.js with Docker

Some commands for development

npm install
npm run-script fix
npm test
npm run

Environment variables

Populate a mqtt-demo/.env file:

MQTT_SERVER_URL=mqtt://mosquitto
MQTT_USERNAME=
MQTT_PASSWORD=

PGHOST=postgres
PGUSER=iot
PGPASSWORD=CRxVcSPsB704
PGDATABASE=iot

Manual commands to experiment

1. Start stack

cd mqtt-demo/
docker-compose pull
docker-compose build --pull
docker-compose up
# When done, [CONTROL]+[C]
docker-compose down --volumes

2. Publish on MQTT

In another process:

docker exec mosquitto mosquitto_pub -t 'test' -m '{"Hello": "World"}'

Optionally, in yet another process, listen to MQTT:

docker exec mosquitto mosquitto_sub -t '#' -v

3. Check data in PostgreSQL

docker exec postgres psql -d iot -U iot -c 'SELECT * FROM mqtt ORDER BY id DESC LIMIT 10;'

4. Manual changes to database

docker exec -it postgres psql -d iot -U iot
\dt
ALTER TABLE mqtt ADD COLUMN comment TEXT;

Or re-run the init script if it was modified:

docker exec postgres psql -d iot -U iot -a -f /docker-entrypoint-initdb.d/mqtt.sql