简体   繁体   English

如何使用 mqtt.js 连接到运行在谷歌云虚拟机实例上的 Mosquitto MQTT Broker

[英]How to connect to Mosquitto MQTT Broker, that is running on a Google Cloud Virtual Machine Instance, using mqtt.js

What I am trying to achieve: I have a Mosquitto MQTT Broker running on a Google Cloud virtual machine (Ubuntu), and I want to be able to connect to it from my local PC using mqtt.js我想要实现的目标:我有一个在 Google Cloud 虚拟机 (Ubuntu) 上运行的 Mosquitto MQTT Broker,我希望能够使用 mqtt.js 从我的本地 PC 连接到它

My setup我的设置

I have created a VM instance in Google Cloud, running Ubuntu 20.04.LTS.我在 Google Cloud 中创建了一个 VM 实例,运行 Ubuntu 20.04.LTS。

Some of the settings:一些设置:

Firewall – allow HTTPS and allow HTTP
Firewall rule – opens port 1883

I installed Mosquitto MQTT Broker (version 1.6.9) on this VM.我在此虚拟机上安装了 Mosquitto MQTT Broker(版本 1.6.9)。

I was able to verify the installation and that it was running, by opening to SSH terminals, one to publish, one to subscribe我能够通过打开 SSH 终端来验证安装并且它正在运行,一个用于发布,一个用于订阅

mosquitto_sub -t test
mosquitto_pub -t test -m “hello”

Then I read that when I want to connect to VMs using third-party tools, I must create and upload my own SSH keys to VMs:然后我读到当我想使用第三方工具连接到 VM 时,我必须创建自己的 SSH 密钥并将其上传到 VM:

ssh-keygen -t rsa -f C:\keys\VM_KEYFILE -b 2048 pwd: ****

I got two files now, the private and public keys:我现在有两个文件,私钥和公钥:

VM_KEYFILE
VM_KEYFILE.pub

I then used icacls to modify the private key's permissions:然后我使用 icacls 修改私钥的权限:

icacls.exe VM_KEYFILE /reset
icacls.exe VM_KEYFILE /grant:r “$($env:username):(r)”
icacls.exe VM_KEYFILE /inheritance:r

I then successfully connected ot the VM from a Windows console:然后我从 Windows 控制台成功连接了虚拟机:

ssh -i "VM_KEYFILE" username@vm_public_ip_address

So now I want to try and connect using node.js所以现在我想尝试使用 node.js 进行连接

I already have a javascript file that uses mqtt.js to connect to some of the public MQTT brokers, eg HiveMQ Some of its settings are:我已经有一个 javascript 文件,它使用 mqtt.js 连接到一些公共 MQTT 代理,例如 HiveMQ 它的一些设置是:

let broker_host = 'broker.hivemq.com';
let broker_port = 1883;
let client_id = 'my_client_1';

const connection_options = {
    port: broker_port,
    host: broker_host,
    clientId: client_id,
    clean: true,
    keepalive: false
};

My question: How would I modify this JavaScript file to connect to the MQTT broker that is running in the Google Cloud VM我的问题:我将如何修改此 JavaScript 文件以连接到在 Google Cloud VM 中运行的 MQTT 代理

There is no username/password/authentication set up for the broker itself, just the VM.没有为代理本身设置用户名/密码/身份验证,只有 VM。

I tried something like this, but I have no idea how to use the SSH key我试过这样的东西,但我不知道如何使用 SSH 密钥

let broker_host_gcm_vm = 'https://<vm_public_ip_address>

UPDATE更新

I can connect to the broker from both (a) MQTT Explorer, and (b) MQTTX deskptop app.我可以从 (a) MQTT Explorer 和 (b) MQTTX 桌面应用程序连接到代理。

All I have to enter for the connection details is:我必须输入的连接详细信息是:

Host: mqtt://<ip address>
Port: 1883

Then I can publish / subscribe successfully.然后我就可以发布/订阅成功了。

I tried changing my JavaScript connection to the following, but I still can't connect from here:我尝试将我的 JavaScript 连接更改为以下,但我仍然无法从这里连接:

let broker_host_gcm_vm1 = 'mqtt://<ip address>';

I found the problem.我发现了问题。

Let's say the host IP address is 11.22.33.44假设主机 IP 地址是 11.22.33.44

The host was none of these:主机不是这些:

let broker_host = 'http://11.22.33.44';
let broker_host = 'https://11.22.33.44';
let broker_host = 'mqqt://11.22.33.44';
let broker_host = 'mqtts://11.22.33.44';

But was simply this:但就是这样:

let broker_host = '11.22.33.44';

Simple when you know how:)当你知道怎么做时很简单:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从本地运行的 NodeJS / TypeORM 连接到 Google Cloud MySQL 实例 - Connect to Google Cloud MySQL instance from local running NodeJS / TypeORM Mosquitto MQTT服务添加SSL配置后重启失败 - Mosquitto MQTT service failed to restart after adding SSL configuration 如何从本地机器连接到谷歌云 VPC - How to connect to Google cloud VPC from on-premise machine 如何在运行在云函数上的 puppeteer 实例上使用 puppeteer.connect - How to use puppeteer.connect on a puppeteer instance, that is running on a cloud function gogole云虚拟机如何添加端口 - How to add ports in gogole cloud virtual machine 在运行连接到 Redis 实例的 Google Cloud Function 时遇到“错误:连接 ECONNREFUSED 127.0.0.1:6379...” - Encountering "Error: connect ECONNREFUSED 127.0.0.1:6379..." when running Google Cloud Function connecting to a Redis Instance Eclipse Paho MQTT Golang 发布订阅 AWS IOT 在同一台机器上作为单独进程运行时返回 EOF 错误 - Eclipse Paho MQTT Golang publish subscribe to AWS IOT returns EOF error when running as separate process on same machine 是否可以使用公共 IP 将 WordPress 网站(托管在 GoDaddy 上)连接到 Google Cloud Platform 实例? - Is it possible to connect WordPress Website (hosted on GoDaddy) to Google Cloud Platform Instance using Public IP? 如何用node.js服务器和aws虚拟机连接到Html页面 - How connect to Html page with node.js server and aws virtual machine MQTT.fx 无法连接到 AWS IoT (MqttException) - MQTT.fx can't connect to AWS IoT (MqttException)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM