Search code examples
node.jsmqttmosca

Bind Mosca listening IP address?


Is there a way to listen for incoming network connection exclusively on a specific ip address/hostname? Preferably dynamically by passing the ip address/hostname in code as opposed to editing a config file.

Could find no reference to this in Mosca's documentation - http://www.mosca.io/docs/ which is why I'm posting.

Thanks for your time.


Solution

  • It is in the docs here. You can pass in both a host and port as part of the options object when creating a new mosca.Server object.

    var pubsubsettings = {
      //using ascoltatore
      type: 'mongo',        
      url: 'mongodb://localhost:27017/mqtt',
      pubsubCollection: 'ascoltatori',
      mongo: {}
    };
    
    var moscaSettings = {
      port: 1883,           //mosca (mqtt) port
      host: "127.0.0.1",
      backend: pubsubsettings   //pubsubsettings is the object we created above 
    
    };
    
    var server = new mosca.Server(moscaSettings);   //here we start mosca
    server.on('ready', setup);  //on init it fires up setup()
    
    // fired when the mqtt server is ready
    function setup() {
      console.log('Mosca server is up and running')
    }