moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

How to fire the events with Mosca/Moquitto ? #656

Closed MatLacoste closed 7 years ago

MatLacoste commented 7 years ago

Hi everyone !!

Well, I'm quite new to mqtt and I have to use it in a NodeJs project. I discovered this really nice library you made and decided to go with it. At first I used Mosca as a stand-alone broker with following code :

var mosca = require('mosca')
var settings = {
    port: 1883,
    persistence: {
        factory:mosca.persistence.Memory
    }
};
 
var server = new mosca.Server(settings, function() {
    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.toString());
});

server.on('subscribed', function(topic, client) {
    console.log('subscribed : ', 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);
});

Everything was working fine (and the events were well fired), but I decided to use Mosca Embedded with Mosquitto instead. so I changed "server" and "settings" as following :

var pubsubsettings = {
    type: 'mqtt',
    json: false,
    mqtt: require('mqtt'),
    host: 'localhost',
    port: 1883
};

var moscaSettings = {
    port: 1883,
    backend: pubsubsettings,
    persistence: {
        factory:mosca.persistence.Memory
    }
};

var server = new mosca.Server(moscaSettings);
server.on('ready', setup);
function setup() {
    console.log('Mosca server is up and running')
}

The problem is that now, besides the setup event, none of the events is able to fire. Moreover, I found out that every console.log for the events fire at once in the node prompt when I stop the Mosquitto Broker Service.

Sorry if it's really basic question (or if I made basic mistakes) but I'm truely deseparate and didn't found an answer for it by googling. Also sorry for bad English and thanks in advance for your help and attention :)

mcollina commented 7 years ago

You can't have both mosca and mosquitto on the same port, 1883.