A Node.js application for connecting to MQTTS brokers with support for Azure IoT Hub integration, client certificates, and comprehensive logging.
This application provides a robust MQTTS client that can connect to MQTT brokers using SSL/TLS encryption. It includes special support for Azure IoT Hub connections with automatic SAS token generation, twin operations, and device-to-cloud messaging.
- ✅ MQTTS (MQTT over SSL/TLS) connection support
- ✅ Azure IoT Hub integration with SAS token authentication
- ✅ Client certificate authentication
- ✅ Device Twin operations (GET)
- ✅ Device-to-cloud messaging
- ✅ Cloud-to-device message reception
- ✅ Comprehensive event logging
- ✅ Configurable connection parameters
- ✅ Automatic test message publishing
- Clone or download the project
- Install dependencies:
npm install
node app.js --host <mqtt_host> --device-id <device_id> [options]
node app.js --iothub <iothub_name> --device-id <device_id> --device-key <device_key> --host <iothub_host>
--host <string>
- MQTT hostname or IP address--device-id <string>
- Device identifier
NOTE: omit those parameters if not connecting to Azure IoT Hub, but to a generic MQTT server.
--iothub <string>
- IoT Hub name (automatically configures username and generates SAS token) - this is required for Azure IoT Hub connections and shall NOT include the.azure-devices.net
suffix.--device-key <string>
- Shared access key for IoT Hub device
NOTE: these parameters are not required for Azure IoT Hub connections.
--port <number>
- MQTTS broker port (default: 8883)--username <string>
- Username for MQTT connection--password <string>
- Password for MQTT connection--mqtt-version-int <number>
- MQTT protocol version (default: 4)
NOTE: these parameters are not required for Azure IoT Hub connections.
--cert <string>
- Path to client certificate file--key <string>
- Path to client private key file--ca <string>
- Path to Certificate Authority file--reject-unauthorized <boolean>
- Verify broker certificate validity (default: false)
node app.js --iothub myiothub --device-id mydevice001 --device-key "devkey" --host myiothub.azure-devices.net
node app.js --host mqtt.example.com --device-id sensor01 --cert client.pem --key client.key --ca root_ca.pem --reject-unauthorized true
node app.js --host secure-mqtt.example.com --device-id device123 --username myuser --password mypassword --port 8883
node app.js --host localhost --device-id testdevice --username admin --password secret --port 8883
You can also use environment variables for sensitive information:
IOTHUB_DEVICE_KEY
- Device key for Azure IoT Hub. Alternative to--device-key
parameterMQTT_PASSWORD
- MQTT password - alternative to--password
parameter - NOT required for Azure IoT Hub connections
Example:
set IOTHUB_DEVICE_KEY=omissis
node app.js --iothub myiothub --device-id mydevice001 --host myiothub.azure-devices.net
When using the --iothub
parameter, the application automatically:
- Generates SAS Token: Creates a Shared Access Signature token for authentication
- Configures Username: Sets the proper username format for Azure IoT Hub
- Subscribes to Twin Updates: Listens for device twin changes (
$iothub/twin/res/#
) - Requests Twin State: Sends a GET request for the current twin state
- Subscribes to C2D Messages: Listens for cloud-to-device messages
- Sends Test Messages: Publishes test events every 20 seconds
The application automatically subscribes to twin response topics and sends a twin GET request:
Topic: $iothub/twin/GET/?$rid=1
Test messages are published to:
Topic: devices/{device-id}/messages/events/
Payload: {"kind": "TestMessage", "time": "2025-01-07T10:30:00.000Z"}
The application subscribes to:
Topic: devices/{device-id}/messages/devicebound/#
The application provides comprehensive logging for:
- Connection events (connect, reconnect, close, offline, error, end)
- Message events (message, packetsend, packetreceive)
- Subscription confirmations
- Publication results
- Error conditions
All events are logged with timestamps and detailed information.
testmqtt/
├── app.js # Main application file
├── build-iothub-sas.js # SAS token generation utility
├── package.json # Project dependencies
└── README.md # This file
- mqtt: MQTT client library
- yargs: Command-line argument parsing
- crypto: Cryptographic functionality (built-in Node.js)
- fs: File system operations (built-in Node.js)
- Certificate Errors: Ensure certificate files are valid and accessible
- Authentication Failures: Verify username/password or device key
- Network Issues: Check host and port accessibility
- Protocol Version: Try different MQTT protocol versions if connection fails
This project is provided as-is for educational and testing purposes.
Feel free to submit issues and enhancement requests!