moscajs / mosca

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

send notification from server side to android client using web browser #623

Closed YasserTahri closed 7 years ago

YasserTahri commented 7 years ago

Hi guys,

i need help, i faced a problem to send notification from my web browser to android client !

can you explain with simple method for sending a notification message from web client

thank you in advance

mcollina commented 7 years ago

How does that fits into Mosca? Can you explain your architecture a bit more?

YasserTahri commented 7 years ago

first , i have installed mosca on my windows server and after i added my broker in mosca and i'm trying to send notification message from my web client interface to ANDROID APP CLIENT

my architecture is : Browser chrome or fire (UI) == send message===> mosca ===> android application My Broker :

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'); }

Part web Client :

can you help me how i can use the best solution to send message from my web client to android app

thank you

mcollina commented 7 years ago

what's not working?

YasserTahri commented 7 years ago

when i tryto select message and clic into button to send my selected message to android app , in other word no notification is happened in android app also in my broker

mcollina commented 7 years ago

Does it work with a standard MQTT client? Have you tried putting a full URL inside mqtt.connect()?

YasserTahri commented 7 years ago

already i have used this MQTT client : https://github.com/mqttjs/MQTT.js

and about mqtt.connect() already i made full URL of my server

YasserTahri commented 7 years ago

Code in my html page :

also when i make full URL always i see this error message :

' failed: Error during WebSocket handshake: Unexpected response code: 200

mcollina commented 7 years ago

In your script, you are not passing a full URL to mqtt.connet(). It should be something like mqtt.connect('ws://{YOURDOMAIN/IP}'),

YasserTahri commented 7 years ago

message in console : WebSocket connection to 'ws://46.21.205.50/' failed: Error during WebSocket handshake: Unexpected response code: 200

mcollina commented 7 years ago

from your example, you are running Mosca on port 3000, not 80.

YasserTahri commented 7 years ago

i changed port 3000 to 80 but same problem :

WebSocket connection to 'ws://46.21.205.50/' failed: Error during WebSocket handshake: Unexpected response code: 200

mcollina commented 7 years ago

you can't 80, as you are probably serving something else there, like your index.html. You need to keep that as 3000, and connect to mqtt.connect('ws://46.21.205.50:3000').

YasserTahri commented 7 years ago

yes i have done mqtt.connect('ws://46.21.205.50:3000') but unfortunetely the same problem

YasserTahri commented 7 years ago

message error : WebSocket connection to 'ws://46.21.205.50:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

mcollina commented 7 years ago

you Mosca instance is not really running and/or exposed at that port.

YasserTahri commented 7 years ago

when i run subscribe and publish on server by command it works but the problem when i run it on web client

mcollina commented 7 years ago

those are served on two separate ports. What is serving your web client?

YasserTahri commented 7 years ago

mosca port http : 3000 web client 80 MQTT port :1883

mcollina commented 7 years ago

so, you need to connect to port 3000 with mqtt.connect().

YasserTahri commented 7 years ago

WebSocket connection to 'ws://46.21.205.50:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

mcollina commented 7 years ago

are you sure that port 3000 is open on that host? Is that host a server?

YasserTahri commented 7 years ago

port 3000 opened and IP is the adress of my server

YasserTahri commented 7 years ago

@mcollina can you tell me about the best architecture and tools that i can use

because i think it's a problem in my architecture

thank you

mcollina commented 7 years ago

very likely, but I have little time, and this is Mosca issue tracker.

YasserTahri commented 7 years ago

already port is verified that it's opened