moscajs / mosca

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

MQTT web client with mosca #607

Closed YasserTahri closed 7 years ago

YasserTahri commented 7 years ago

Hi guys,

I need to make a push to send a message from my interface to android client, already i have installed mosca and using MQTT web client but when i want to publish i see a message in my console : "connection to 'ws://xxx.aggema.com/' failed: Error during WebSocket handshake: Unexpected response code: 200"

mcollina commented 7 years ago

Hard to tell, probably you configured something wrongly on the HTTP/WebSocket layer.

YasserTahri commented 7 years ago

ok, please can you tell me how i will make a correct config on the HTTP/WebSocket layer.

Thank you

mcollina commented 7 years ago

how are you doing it right now?

YasserTahri commented 7 years ago

my brocker.js

var mosca = require('mosca')

var ascoltatore = { //using ascoltatore type: 'mongo',
url: 'mongodb://localhost:27017/agg_mqtt', pubsubCollection: 'ascoltatori', mongo: {} };

var moscaSettings = { backend: ascoltatore, http: { port: 3000, bundle: true, static: './' }, persistence: { factory: mosca.persistence.Mongo, url: 'mongodb://localhost:27017/agg_mqtt' } }; var server = new mosca.Server(moscaSettings); server.on('clientConnected', function(client) { console.log('client connected', client.id); }); server.on('subscribed', (topic, client) => { server.publish(message, client) })

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

server.on('ready', setup);

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

mcollina commented 7 years ago

what's your code on the client?

that should work from the browser. Which browser are you using?

YasserTahri commented 7 years ago

I have linked mqtt.js already exist in public folder in mosca to my html page my browser is : google chrome my MQTT web client :

var client = mqtt.connect(); var sub = client.subscribe("/myTopic"); client.on("message", function(topic, payload) { alert([topic, payload].join(": ")); client.end(); }); client.publish("/myTopic","test");

thx

YasserTahri commented 7 years ago

WebSocket connection to 'ws://xxx.aggema.com/' failed: Error during WebSocket handshake: Unexpected response code: 200

mcollina commented 7 years ago

are you serving this through NGINX? What's that domain name?

YasserTahri commented 7 years ago

xxx.aggema.com/ : it's domaine of my web site with apache

mcollina commented 7 years ago

The problem is apache. It does not support Websockets.

mcollina commented 7 years ago

You need to use websockets. You can fire Mosca on another domain/port and connect it there.

YasserTahri commented 7 years ago

i have used mqtt.js client , have you an idea about others web client mqtt

thx

mcollina commented 7 years ago

it's a matter of infrastructure and protocol.

YasserTahri commented 7 years ago

Great, i want to send a message from my web site to android client using mqtt protocol and mosca broker

aaryadev commented 6 years ago

@YasserTahri for same I created nodejs websocket bridge so anything published on webpage will be relay to mqtt and reverse.

also you can tweak mosca server to do both