moscajs / mosca

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

How to connect to mosca broker #654

Open Swapnilchavan18 opened 7 years ago

Swapnilchavan18 commented 7 years ago

I have create mosca server with simple code server.js as below, I run it as nodejs app via node server.js I have running website for ex test.com with nginx. How to connect to mosca from client? When I try to connect it takes long time of 1m and request times out.

Server.js code is as below ` var mosca = require('mosca')

var settings = {
  port: 1883
};

//here we start mosca
var server = new mosca.Server(settings);
server.on('ready', setup);

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}

// fired whena  client is connected
server.on('clientConnected', function(client) {
  console.log('message from server == client connected', client.id);
});

// fired when a message is received
server.on('published', function(packet, client) {
  console.log('message from server == Published : ', packet.payload);
});

// fired when a client subscribes to a topic
server.on('subscribed', function(topic, client) {
  console.log('message from server == subscribed : ', topic);
});

// fired when a client subscribes to a topic
server.on('unsubscribed', function(topic, client) {
  console.log('message from server == unsubscribed : ', topic);
 });

// fired when a client is disconnecting
server.on('clientDisconnecting', function(client) {
  console.log('message from server == clientDisconnecting : ', client.id);
});

// fired when a client is disconnected
server.on('clientDisconnected', function(client) {
  console.log('message from server == clientDisconnected : ', client.id);
});`

and with another node app which I run on local machine I have code as below.

` var mqtt = require('mqtt'); var options = { port: 1883, host: 'mqtt://www.test.com', keepalive: 60, reconnectPeriod: 1000, protocolId: 'MQIsdp', protocolVersion: 3, clean: true, encoding: 'utf8' }; this.client = mqtt.connect('mqtt://www.test.com', options);

            this.client.on('connect',  () => {
        this.client.subscribe('presence')
          this.client.publish('presence', 'Hello mqtt')
        })`

It doesnt connect and gets timed out.

mcollina commented 7 years ago

How have you configured your NGINX?

bhavik5 commented 5 years ago

On your local machine, inside this.client = mqtt.connect('mqtt://www.test.com', options); use mqtt://SERVER_URL.

After that publish a message on a specific topic from the server, like below,

  var message = {
    topic: '/myPersonalTopic',
    payload: 'abcde',
    qos: 0,
    retain: false
  };

  server.publish(message, function () {
    console.log('done!');
  });

And then connect client, and subscribe to a topic in which message is published earlier, like below

  client.on('connect', function () {
    client.subscribe('/myPersonalTopic', function (err) {
      if (!err) {
        client.publish('/myPersonalTopic', 'Message Received')
      }
    })
  })