node.js - MQTT-over-Websockets doent work on remote server -
i'm using deploy https://medium.com/@lelylan/how-to-build-an-high-availability-mqtt-cluster-for-the-internet-of-things-8011a06bd000
now want send message form browser device (for fake in localhost) if
both broker client.hmtl , client.js on localhost
client.html
<html> <head> <meta charset="utf-8" /> </head> <body> <script src="./browsermqtt.js"></script> <script> var device = { nickname: 'wbk0da8v9l2wewmi', secret: 'mysecret' }; var msg = 'devices/' + device.nickname + '/msg' ; var var client = mqtt.connect( { host: 'localhost', port: 3000, username: device.nickname, password: device.secret }); client.subscribe(msg); client.on('message', function(topic, payload) { console.log('my message ',[topic, payload].join(": ")); client.end(); }); client.publish(msg, "hello world client html !"); </script> </body> </html>
client.js
var device = { nickname: 'wbk0da8v9l2wewmi', secret: 'mysecret' }; var msg = 'devices/' + device.nickname + '/msg' ; var mqtt = require('mqtt'); console.log(msg); var client = mqtt.connect('mqtt://localhost:1883',{ username: device.nickname, password: device.secret }); client.subscribe(msg); client.on('connect', function() { console.log('client connected'); client.on('message', function(topic, message) { console.log('received from', topic, message); client.publish(msg, 'client 1 alive.. test ping msg! ' + date()); client.end(); }); });
it works fine.
but if have broker , client.html on remote server leaving client.js on localhost with
var device = { nickname: 'wbk0da8v9l2wewmi', secret: 'mysecret' }; var msg = 'devices/' + device.nickname + '/msg' ; var mqtt = require('mqtt'); console.log(msg); var client = mqtt.connect('mqtt://204.151.191.103:1883',{ username: device.nickname, password: device.secret }); client.subscribe(msg); client.on('connect', function() { console.log('client connected'); client.on('message', function(topic, message) { console.log('received from', topic, message); client.publish(msg, 'client 1 alive.. test ping msg! ' + date()); client.end(); }); });
it doesn't work.
i'm wondering what's problem ? i'm doing wrong ?
update
when doen't work mean
i can see
client connected
from console window (when run client.js)
but can't see message both client
received from
(the on message event isn't triggered)
and browser side
my message
(the on message event isn't triggered)
when run client.html in firebug console i've got
firefox can't establish connection server @ ws://myserver.me:3000/.
on client.html did change host: 'localhost', port: 3000 remote 1 ?
did connect sucessfully 204.151.191.103 port 1883 mqtt client mqtt.fx or mosquitto_pub ?
Comments
Post a Comment