How to set proxy parameters using MQTT protocol link, for example Azure

background

On some corporate networks, a firewall is set up to block the interaction between the Internet and the corporate network. If an internal application needs to connect to an external network, the corporate layer needs to provide a proxy address.

The MQTT protocol

MQTT: Message Queue Telemetry Transport protocol based on TCP/IP or other lossless bidirectional network links. It is a client-server publish/subscribe messaging protocol that is widely used for communication in such machine-to-machine (M2M) and Internet of Things (IoT) environments.

The MQTT SDK sets up the agent

MQTT protocol does not support proxy. We need to configure proxy in this way according to the parameters of MQTT protocol based on Web socket, which can achieve proxy service at the HTTP level. Various SDK manufacturers should provide similar SDK such as AzureMQTT-CONNECT-SDK

Use the MQTTWS NODE example

  • Reference https-proxy-agent package Https-proxy-agent: acts as the message proxy at the HTTP or HTTPS layer.
  • Reference MqttWs Package MqttWs: The MQTT SDK provided by Azure

The Demo is as follows:

var HttpsProxyAgent = require('https-proxy-agent')
var Protocol = require('azure-iot-device-mqtt').MqttWs
var Client = require('azure-iot-device').Client

var client = Client.fromConnectionString(connectionString, Protocol)

client.setOptions(
 {
   mqtt: {
     webSocketAgent: new HttpsProxyAgent(`${proxy}`),}})Copy the code