首页 文章

是否可以将MQTT客户端连接到具有Node.JS作为中间网关的RabbitMQ?

提问于
浏览
0

这就是我想要做的:

我想使用 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 回答

  • 3

    你有不同的可能性,只是弄清楚其中两个:

    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

  • 1

    刚刚实现了同样的目标希望答案有所帮助

    var mosca = require("mosca");
    var mqtt = require('mqtt');
    
    var pubsubsettings = {
        type: 'mqtt',
        json: false,
        mqtt: require('mqtt'),
        clientId: 'MqttBrokerPublisher',
        username: '<USERNAME>',
        password: '<PASSWORD>',
        host: '<YOUR_HOST>',
        port: 1883,
    };
    
    var moscaSettings = {
        //port: 1883,
        backend: pubsubsettings,
        http: {
            port: 3000,
            bundle: true,
            static: './'
        }
    }
    
    
    var server = new mosca.Server(moscaSettings);
    
    server.on('ready', function(){
        console.log('connected to the server');
    });
    
    server.on('connect', function(){
        console.log('connected to the server');
    });
    
    server.on('clientConnected', function(client) {
        console.log('client connected', client.id);
    });
    
    server.on('published', function(packet, client) {
      console.log('Published', packet.payload);
    });
    
    server.on('error', function(err) {
      console.log('error', err);
    });
    

    Explaination:

    在上面的代码我使用混合移动应用程序作为MQTT客户端(Cordova应用程序),只使用websocket使用mqtt.js

    在撰写本文时,没有支持iOS的cordova插件 . 仅限Android的插件可用 . 点击此链接

    所以我使用 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交谈 .

相关问题