moscajs / mosca

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

Web-socket not receiving or publishing message #521

Closed prayagvakharia closed 7 years ago

prayagvakharia commented 8 years ago

I am trying web client to connect with mosca. it will connect successfully but not able to publish or subscribe any topic via web.

Server:

var mosca = require('mosca');
var pubsubSettings = {
    /* For mongodb */
    type: 'mongo',
    url: 'mongodb://localhost:27017/mqtt',
    pubsubCollection: 'mqttTest',
    mongo: {}
};
var moscaSetting = {
    interfaces: [
        { type: "mqtt", port: 1883 },
        { type: "http", port: 3000, bundle: true, static: './' }
    ],
    stats: false,
    logger: { name: 'MoscaServer', level: 'debug' },
    backend: pubsubSettings,
};

var server = new mosca.Server(moscaSetting);
server.on('ready', setup);

function setup() {
    console.log('Mosca server is up and running.');
}

Web Client:

<html>
  <head>
    <script src="../../node_modules/mosca/public/mqtt.js"></script>
  </head>
  <body>
    <script>

      var client = mqtt.connect('http://localhost:3000');
      client.subscribe("mqtt/demo");
      client.on("message", function(topic, payload) {
        console.log([topic, payload].join(": "));
      });
      client.publish("mqtt/demo", "hello from web page");
    </script>
    <h1> Hello World!! </h1>
  </body>
</html>

Node Client:

var mqtt = require('mqtt')

client = mqtt.createClient(1883, 'localhost');

client.subscribe('mqtt/demo');
client.publish('mqtt/demo', 'Hello from node client');

client.on('message', function (topic, message) {
  console.log(message.toString('UTF-8'));
});

Not sure whats going wrong i have tried with http, ws too.

i am using mosca -V : 2.0.1 node -v : v6.2.2

Thanks

mcollina commented 8 years ago

what are the logs saying?

prayagvakharia commented 8 years ago

Server Log when Web client is trying to ping:

{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"pingreq","time":1468655466651,"client":"mqttjs_07e06b30","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"setting keepalive timeout","time":1468655466651,"timeout":15000,"client":"mqttjs_07e06b30","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"pingreq","time":1468655469064,"client":"mqttjs_03d38594","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"setting keepalive timeout","time":1468655469064,"timeout":15000,"client":"mqttjs_03d38594","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"pingreq","time":1468655476661,"client":"mqttjs_07e06b30","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"setting keepalive timeout","time":1468655476661,"timeout":15000,"client":"mqttjs_07e06b30","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":30,"msg":"keepalive timeout","time":1468655477291,"client":"mqttjs_8cbd71a3","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"closing client, reason: keepalive timeout","time":1468655477291,"client":"mqttjs_8cbd71a3","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":30,"msg":"closed","time":1468655477291,"client":"mqttjs_8cbd71a3","v":1}
Client Disconnected     :=  mqttjs_8cbd71a3
Published := { topic: '$SYS/BJ-rLwvw/disconnect/clients',
  payload: 'mqttjs_8cbd71a3',
  messageId: 'BkauwPvw',
  qos: undefined,
  retain: undefined }
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"setting keepalive timeout","time":1468655478347,"timeout":15000,"client":"mqttjs_8cbd71a3","v":1}
{"pid":55534,"hostname":"ubuntu","name":"MoscaServer","level":30,"msg":"client connected","time":1468655478348,"client":"mqttjs_8cbd71a3","v":1}
Client Connected    :=  mqttjs_8cbd71a3
Published := { topic: '$SYS/BJ-rLwvw/new/clients',
  payload: 'mqttjs_8cbd71a3',
  messageId: 'rJCuwvPP',
  qos: undefined,
  retain: undefined }

Logs when Node Client trying to ping server:

{"pid":55630,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"setting keepalive timeout","time":1468655622592,"timeout":15000,"client":"mqttjs_773baeee","v":1}
{"pid":55630,"hostname":"ubuntu","name":"MoscaServer","level":30,"msg":"client connected","time":1468655622596,"client":"mqttjs_773baeee","v":1}
Client Connected    :=  mqttjs_773baeee
{"pid":55630,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"setting keepalive timeout","time":1468655622617,"timeout":15000,"client":"mqttjs_773baeee","v":1}
{"pid":55630,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"subscribe received","time":1468655622618,"packet":{"messageId":31951,"qos":1,"subscriptions":[{"topic":"mqtt/demo","qos":0}]},"client":"mqttjs_773baeee","v":1}
{"pid":55630,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"setting keepalive timeout","time":1468655622620,"timeout":15000,"client":"mqttjs_773baeee","v":1}
{"pid":55630,"hostname":"ubuntu","name":"MoscaServer","level":30,"msg":"subscribed to topic","time":1468655622624,"topic":"mqtt/demo","qos":0,"client":"mqttjs_773baeee","v":1}
Subscribed := undefined
Published := { topic: '$SYS/rk5Z_PvD/new/clients',
  payload: 'mqttjs_773baeee',
  messageId: 'HJyz_PDv',
  qos: undefined,
  retain: undefined }
{"pid":55630,"hostname":"ubuntu","name":"MoscaServer","level":20,"msg":"published packet","time":1468655622660,"packet":{"messageId":"Bkx1GdPvD","topic":"mqtt/demo"},"client":"mqttjs_773baeee","v":1}
Published := { topic: 'mqtt/demo',
  payload: <Buffer 48 65 6c 6c 6f 20 66 72 6f 6d 20 6e 6f 64 65 20 63 6c 69 65 6e 74>,
  messageId: 'Bkx1GdPvD',
  qos: 0,
  retain: false }
Published := { topic: '$SYS/rk5Z_PvD/new/subscribes',
  payload: '{"clientId":"mqttjs_773baeee","topic":"mqtt/demo"}',
  messageId: 'SJbyzuvvw',
  qos: undefined,
  retain: undefined }
mcollina commented 8 years ago

If you remove the mongodb configuration, does this work?

prayagvakharia commented 8 years ago

No luck with escap mongoDB configuration.

Also i have tried one more thing: remove setting from web client so now i am able to publish message from web client to node client. but reverse node client to web client still i am not able to subscribe any messages.

only change i did is remove settings from web client.

prayagvakharia commented 8 years ago

One more thing i have observed is:

when i run node client it will setup subscribe topic:

Published := { topic: '$SYS/BklFFSnt/new/subscribes',
  payload: '{"clientId":"mqttjs_b01acd2d","topic":"mqtt/demo"}',
  messageId: 'H1WgqYr3F',
  qos: undefined,
  retain: undefined }

but when run web client there is no subscribes topic is created.

mcollina commented 8 years ago

Which browser are you testing this with?

mcollina commented 7 years ago

Closing for low activity