这就是我想要做的:
我想使用 MQTT
连接我的客户端到 Node.JS server
然后从那里(Node.JS服务器)我想处理所有pub / sub . 当客户端发布任何消息时,它将转到 Node.JS server
并从那里发送到 RabbitMQ
并反向为订户 .
流程如下: -
MQTT - > Node.JS - > RabbitMQ .
Edit:
Server.Js
var mosca = require('mosca')
var pubsubsettings = {
type: 'mqtt',
json: false,
mqtt: require('mqtt'),
host: '127.0.0.1',
port: 1883
};
var settings = {
port: 1883,
backend: pubsubsettings
};
var server = new mosca.Server(settings);
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running')
}
server.on('clientConnected', function (client) {
console.log('client connected', client.id);
});
server.on('published', function (packet, client) {
console.log('Published : ', packet.payload);
});
server.on('subscribed', function (topic, client) {
console.log('subscribed : ', topic);
});
// fired when a client subscribes to a topic
server.on('unsubscribed', function (topic, client) {
console.log('unsubscribed : ', topic);
});
server.on('clientDisconnecting', function (client) {
console.log('clientDisconnecting : ', client.id);
});
server.on('clientDisconnected', function (client) {
console.log('clientDisconnected : ', client.id);
});
这是我在 mosca
主页上找到的 . 我为 RabbitMQ
连接添加了几行 .
现在我想创建一个可以连接到这个 Node.JS
服务器的客户端 . 但我无法弄清楚连接的方式 .
2 回答
你有不同的可能性,只是弄清楚其中两个:
a)你在尝试这个吗?
MQTT客户端A - > Node.js / mosca Broker 1桥接 - > RabbitMQ Broker 2
b)为什么不这个?
两个客户端都可以通过在Broker中发布和订阅相同的主题来交换消息
MQTT客户端A < - > RabbitMQ代理1 < - > Node.js / MQTT.js客户端B
要实现MQTT客户端,请查看:https://github.com/mqtt/mqtt.github.io/wiki/libraries
刚刚实现了同样的目标希望答案有所帮助
Explaination:
在上面的代码我使用混合移动应用程序作为MQTT客户端(Cordova应用程序),只使用websocket使用mqtt.js
所以我使用 Mqtt.js 的websocket实现,它支持在cordova webview /浏览器上运行 iOS (9.3+) and Android(4.4.4+)/Cross-walk
这里Mosca用作 Node.js Pub / Sub http服务器网关,它连接到我的 Mosquitto-RabbitMQ 后端 . 最后,我的移动应用程序与mosca交谈,后者又与Mosquitto交谈 .