Closed pokal4u closed 8 years ago
Looks like you're trying to tunnel your WebSocket connection through nginx, I'd suggest that you try and get it talking to an instance of mosquitto with websockets first to make sure that your client code works. Try iot.eclipse.org on port 80?
Also, you're not constructing the client object correctly: https://www.eclipse.org/paho/files/jsdoc/symbols/Paho.MQTT.Client.html#constructor
The port number is a separate argument..
Try following the basic example on the README.md here: https://github.com/eclipse/paho.mqtt.javascript
// Create a client instance
client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");
HI, Thanks for reply, I tried iot.eclipse.org on port 80 working fine
When I try to connect like this
var client = new Paho.MQTT.Client(location.hostname, Number(location.port), new Date().getTime());
getting error:
Uncaught Error: AMQJS0013E Invalid argument 1463040256047 for clientId.
Thanks
The client ID needs to be a string ;-)
if client ID given string getting error
WebSocket connection to 'ws://www.doamin.com:0/mqtt' failed: Error in connection establishment: net::ERR_ADDRESS_INVALID
var client = new Paho.MQTT.Client("doamin.com", 8010, "myClientId" + new Date().getTime());
Now getting invalid arguments error
I'm using the following in my examples:
client = new Paho.MQTT.Client("iot.eclipse.org", Number(80), "clientId");
So you would use...
client = new Paho.MQTT.Client("doamin.com", Number(8010), "myClientId" + new Date().getTime());
still getting same error "invalid arguments"
Maybe have a play with the live example running here: http://jsfiddle.net/jpwsutton/LkjdbqL3/7/
I tried this, getting same error here also
HI, This github also using paho mqtt.
What is the line below:
eclipse/paho.mqtt.javascript:marked-as-read:154401481
Hi,
As far as I know, GitHub are not using MQTT. What you are seeing in your console is just plain WebSocket communication between this page and the GitHub Server regarding this GitHub Issue.
The code in the jsfiddle I sent you should definitely work, screenshot below:
when given this "iot.eclipse.org", my code was working fine.
Only problem with when I replace with my domain.
is any required installs and config settings
Ok, so it sounds like you're broker isn't configured correctly. What MQTT Broker (server) are you using?
I did't understand
any ex: servers?
we are using nginx server.
Ok, In order to use MQTT, you need to have an MQTT Server running. I recommend that you check out the MQTT community wiki first to get the basics of what MQTT is and how to use it: https://github.com/mqtt/mqtt.github.io/wiki
Once you're happy with that, You'll need an MQTT Server to run, Mosquitto is a popular choice and is also part of Eclipse (http://mosquitto.org/). You'll then need to configure it to use WebSockets, so in the mosquitto configuration file, add the following lines to the bottom:
listener 80
protocol websockets
(You can replace the 80 with whatever port you want to use)
ok will try and let you know
mosquitto config file
listener 8883 10.164.131.198 persistence true persistence_location /var/lib/mosquitto/ persistence_file mosquitto.db log_dest syslog log_dest stdout log_dest topic log_type error log_type warning log_type notice log_type information connection_messages true log_timestamp true allow_anonymous false password_file /etc/mosquitto/pwfile
if any changes done how to restart mosquitto?
On linux, you can always kill the mosquitto process and start it again.
what about config file? I added listener 80 domain.com instead of listener 8883 10.164.131.198.
Still getting same error
after restart mosquitto getting responsde like:
1463121957: Using default config.
1463121957: Opening ipv4 listen socket on port 1883.
1463121957: Opening ipv6 listen socket on port 1883.
1463122003: New connection from 122.175.35.75 on port 1883.
1463122003: Socket error on client
You need to define the protocol as WebSockets, as in my example above:
listener 80
protocol websockets
I added
listener 80 protocol websockets persistence true persistence_location /var/lib/mosquitto/ persistence_file mosquitto.db log_dest syslog log_dest stdout log_dest topic log_type error log_type warning log_type notice log_type information connection_messages true log_timestamp true allow_anonymous false password_file /etc/mosquitto/pwfile
Did that work?
no
Finally:
config files and output
nginx:
map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream websocket { server doamin.com:8010; }
server { listen 8020; server_name domain.com; location / { proxy_pass http://websocket; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $host; }
}
mosquitto:
listener 9001 protocol websockets persistence true persistence_location /var/lib/mosquitto/ persistence_file mosquitto.db log_dest syslog log_dest stdout log_dest topic log_type error log_type warning log_type notice log_type information connection_messages true log_timestamp true allow_anonymous false password_file /etc/mosquitto/pwfile
connection: var client = new Paho.MQTT.Client("ws://doamin.com:8010/websocket", "myClientId" + new Date().getTime());
or
var client = new Paho.MQTT.Client("domain.com", Number(8010), "myClientId" + new Date().getTime());
response: in browser: TypeError: invalid arguments
in server:
1463126188: Opening websockets listen socket on port 9001.
1463126226: Socket error on client
It looks like your Nginx Configuration thinks that the mosquitto broker is listening on 8010, but your mosquitto configuration is actually listening on 9001.
I changed to upstream websocket { server doamin.com:9001; }
listener 9001 protocol websockets
var client = new Paho.MQTT.Client("domain.com", Number(9001), "myClientId" + new Date().getTime());
now response:
mosquitto version 1.4.8 (build date 2016-05-13 12:53:34+0530) starting
1463129581: Config loaded from /etc/mosquitto/mosquitto.conf.
1463129581: Opening websockets listen socket on port 9001.
1463129612: Socket error on client
the port number you're now using in the Javascript isn't the one that nginx is listening on and so at this point, you're now completely bypassing nginx. Maybe try using this web tool to connect to your mosquitto server to make sure that it's correctly listening: http://www.eclipse.org/paho/clients/js/utility/index.html
Once you're sure that mosquitto is correctly listening to websocket connections, then you can try working your way back up to using nginx (if you really need it?). Finally you can start trying with your own code with what you know is a working server.
I checked with above link
9001 port error: Failed to connect: AMQJS0008I Socket closed. 8010 port error: Failed to connect: AMQJSC0001E Connect timed out.
I tried with my code using server: iot.eclipse.org working fine.
When try my server getting errors
Afer few minutes server side getting
1463133465: Saving in-memory database to /var/lib/mosquitto/mosquitto.db. 1463135265: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
Your configuration requires that you are using a valid username and password:
allow_anonymous false
password_file /etc/mosquitto/pwfile
Are you supplying them correctly? Might be worth setting allow_anonymous
to true to see if that works?
http://mosquitto.org/man/mosquitto-conf-5.html
Thanks Working now
HI,
can I use tcp instead of websockets? if possible how?
Thanks
The Browser JavaScript client can only connect using WebSockets (This is due to how WebBrowsers work, not the client), if you need to connect using TCP you'll need to use a different language e.g. the Java client or Python client
Actually we have android application also. When using Websocket working in mobile and desktop versions ,android not working. So how to configure TCP in config files (nginx,mosquitto)? And can we run both on different ports or same ports?
nginx is only for web applications so it's only useful in the context of WebSockets, in mosquitto you just add the following lines to your config file to enable tcp:
listener 1883
protocol mqtt
And you can only use different ports.
I recommend that you read the mosquitto man pages : http://mosquitto.org/man/mosquitto-conf-5.html
I added websockets and tcp on defferent ports. Connected client on android and desktop. but when sending messages did't get form desktop to android and android form desktop. We are getting messages from desktop to desktop and android to android
please look into this once
It sounds like you are either publishing and subscribing to different topics, or you are running two separate instances of your MQTT server. Have you tried this using the iot.eclipse.org server first to rule out your own setup?
we use iot.eclipse.org to connect in desktop on 80 port and android on 1883 port, but response was same
Right, sounds like you're publishing / subscribing to the wrong topics then maybe. What topics are you publishing / subscribing to on your clients?
code: test1.html: client.subscribe("/srinivas"); message = new Paho.MQTT.Message("hi"); message.destinationName = "/pokala"; client.send(message);
test2.html: client.subscribe("/pokala"); message = new Paho.MQTT.Message("hello"); message.destinationName = "/srinivas"; client.send(message);
please check code is ok?
That looks, fine, what about your Android topics?
package pahotest.demo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String broker = "tcp://iot.eclipse.org:1883";
String clientId = "Aone";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
sampleClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
((TextView) findViewById(R.id.msg)).setText(((TextView) findViewById(R.id.msg)).getText().toString() + "\n" + mqttMessage.toString());
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
Log.i("Paho", "Connecting to broker: " + broker);
sampleClient.connect(connOpts);
Log.i("Paho", "Connected");
Log.i("Paho", "Publishing message: " + "Hello");
MqttMessage message = new MqttMessage("Hello".getBytes());
message.setQos(0);
sampleClient.publish("srinivas", message);
sampleClient.subscribe("pokala", 0);
Log.i("Paho", "Message published");
} catch (MqttException me) {
Log.i("Paho", "reason " + me.getReasonCode());
Log.i("Paho", "msg " + me.getMessage());
Log.i("Paho", "loc " + me.getLocalizedMessage());
Log.i("Paho", "cause " + me.getCause());
Log.i("Paho", "excep " + me);
me.printStackTrace();
}
}
}
Your javascript clients are publishing and subscribing to "/srinivas" and "/pokala", but your android clients are publishing and subscribing to "srinivas" and "pokala". These are not the same topics and so this is why your clients are not talking to each other, you will need to use the exact strings in this case.
Thanks you very much
Working fine....
Hi,
How to configure username and password.
We can set username and password in password_file /etc/mosquitto/pwfile ? or create a username and password for every user?
At this point, this issue has veered a long way off course from the original problem. For questions like this I'd recommend going to the Eclipse Mosquitto forum.
But first, go take a look at the documentation for Mosquitto:
Those pages contain exactly what you need to configure user authentication. Finally, this exact question has been asked on stack overflow a lot, so I'd suggest trying to find them on google.
HI,
I try to create a application using paho mqtt javascript.
My config file:
http { map $http_upgrade $connection_upgrade { default upgrade; '' close; }
upstream websocket { server domain.com:8010; }
server { listen 8020; location / { proxy_pass http://websocket; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } server { listen 80; server_name domain.com ; server_tokens off;
}
}
code:
var client = new Paho.MQTT.Client("ws://domain.com/websocket", "myClientId" + new Date().getTime());
response:
And I tried
var client = new Paho.MQTT.Client("ws://domain.com:8020/websocket", "myClientId" + new Date().getTime());
And finally var client = new Paho.MQTT.Client("ws://domain.com:8010/websocket", "myClientId" + new Date().getTime());
getting response like:
PLease help me
Thanks