MQTT 服务器代理比较:https://github.com/mqtt/mqtt.github.io/wiki/server-support
node.js
MQTT服务端:
const mosca = require('mosca');
var ascoltatore = {
//using ascoltatore
//type: 'mongo',
//url: 'mongodb://localhost:27017/mqtt',
//pubsubCollection: 'ascoltatori',
//mongo: {}
};
var settings = {
port: 1883,
backend: ascoltatore
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published', packet.payload);
});
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running');
}
MQTT Client:
const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://127.0.0.1');
client.on('connect',function(){
client.subscribe('presence');
client.publish('presence','Hello mqtt');
});
client.on('message',function(topic,message){
console.log(topic.toString());
console.log(message.toString());
client.end();
})
安装MQTT服务器——ActiveMQ:
下载:
http://activemq.apache.org/components/classic/download/
解压后到bin目录下直接activitemq start
安装MQTT服务器——rabbitmq
下载地址:https://www.rabbitmq.com/install-windows.html
安装前需要先安装erlang
安装MQTT服务器——mosquitto