Settings proxy with MQTT

How to set proxy parameters using MQTT protocol link, take Azure as an example

background

At some corporate network levels, a layer of firewalls will be set up to block some interactions between the Internet and the corporate network. If internal applications need to link with external networks, the corporate layer needs to provide a layer of proxy addresses.

MQTT protocol

MQTT: Message Queue Telemetry Transport message queue telemetry transport protocol based on tcp/ip or other lossless two-way network links. It is a client-server publish/subscribe message transport protocol, widely used such as machine-to-machine (M2M) and in the way of communication in the Internet of Things (IoT) environment.

MQTT SDK setup broker

The MQTT protocol does not support the proxy method. We need to configure the proxy in this way according to the parameters of the MQTT protocol based on Web sockets. We can achieve proxy services at the HTTP level. All kinds of SDK manufacturers should provide them. A similar SDK such as Azure MQTT-CONNECT-SDK Screen Shot 2022-03-29 at 1.41.19 PM.png

Example using MQTTWS NODE

  • Refer to https-proxy-agent package https-proxy-agent : Responsible for the message proxy at the http/https level.
  • Reference MqttWs package MqttWs : MQTT SDK provided by Azure

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}`),
   },
 }
)
复制代码

Guess you like

Origin juejin.im/post/7080394287828959246
Recommended