sclausen / ngx-mqtt

This library isn't just a wrapper around MQTT.js for angular. It uses observables and takes care of subscription handling and message routing.
https://sclausen.github.io/ngx-mqtt/
MIT License
186 stars 82 forks source link

Error during WebSocket handshake: net::ERR_CONNECTION_RESET #24

Closed theunreal closed 7 years ago

theunreal commented 7 years ago

I'm trying to connect to my mqtt server (ec2 instance) from angular 2 using this library, but I get the following error:

WebSocket connection to 'ws://my-ec2:1883/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

I managed to puslish from my computer, so there is no connection problem.

What configuration am I missing?

sclausen commented 7 years ago

Do you publish from your computer the same way, you try to connect with ngx-mqtt?

theunreal commented 7 years ago

@sclausen In my server:

mosquitto_pub -h localhost -p 1883 -q 1 -d -t localgateway_to_awsiot -i clientid1 -m "{\"key\": \"helloFromLocalGateway\"}"

In my computer I changed localhost to the server ip.

with ngx-mqtt I am trying to use the following configuration:

export const MQTT_SERVICE_OPTIONS = {
  hostname: 'my-server-ip',
  port: 1883,
  path: ''
};
sclausen commented 7 years ago

Since you have port 1883 on your PC and in the ngx-mqtt config as well, you're probably mixing the transport protocols. You need to have a websocket enabled mqtt broker to connect with your browser. The broker commonly provides a websocket endpoint via a different port. See this blog post for a mosquitto example http://blog.ithasu.org/2016/05/enabling-and-using-websockets-on-mosquitto/. I have absolutely no experience with ec2 and don't know how mqtt and mqtt via websockets work with aws iot though. Since this seems not to be a problem with my library, but how to use aws iot mqtt with websockets, you should look at the official examples. Maybe this repository will help https://github.com/awslabs/aws-iot-examples

theunreal commented 7 years ago

@sclausen seem like I configured my server to allow websockets (built mosquitto with websocket on)

now I get another error when placing the service in my app:

stream.js:77 WebSocket connection to 'ws://server:9001/' failed: Connection closed before receiving a handshake response

Mosquitto config:

listener 1883
protocol mqtt

listener 9001
protocol websockets

Do you have any idea what's could have gone?

sclausen commented 7 years ago

Are you connecting through a proxy?

Sorry, this is really hard to remote-debug, since it's not a problem with the library, but your whole IT setup (network/broker)

theunreal commented 7 years ago

Not connecting through proxy, just a new angular 4 app. anyway the port should be 1883 or 9001?

I tried to google for any possible solution and couldn't find one. Annoying how hard is it to setup a basic mosquitto server with web sockets.

marekburiak commented 7 years ago

I haven't had much issues connecting using websockets. My config:

# MY CUSTOM LISTENERS

listener 1883

listener 9001 127.0.0.1
protocol websockets

I am connecting with http://www.hivemq.com/demos/websocket-client/ on port 9001 and also by using this library just fine. Do you have port 9001 open in firewall on the "server"? Are you even able to connect to it on port 9001 from another client (like that hivemq)?

sclausen commented 7 years ago

The port should be 9001 then, because it's the one the broker listens for websocket. Do you tried sudo netstat -tulpen | grep -v 9001 to see, if something really listens on 9001? If you compiled mosquitto with websockets enabled for yourself, it should work though.

To minimize any source of mistakes, you couldtry to set up a basic websocket connection with MQTT.js alone. https://github.com/mqttjs/MQTT.js There is also a section describing how to use MQTT.js in the browser. If you can't run a small example with websockets in the browser, there should be something wrong with your setup and ngx-mqtt wouldn't work anyway.

theunreal commented 7 years ago

@marekburiak @sclausen Thank you guys! but I just tried to connect to public mqtt websocket client such as this one using this library. config:

export const MQTT_SERVICE_OPTIONS = {
  hostname: 'broker.mqttdashboard.com',
  port: 8000,
  path: ''
};

and I get:

WebSocket connection to 'ws://broker.mqttdashboard.com:8000/' failed: Connection closed before receiving a handshake response

Edit: It works using Paho.

So it seem like not a problem with the server. I have no clue what I'm missing here.

kishandonepudi commented 5 years ago

any solution for this, i am also getting same error.

this.topic = "indus/TW-BM-02"; // Create a client instance let client = new Paho.MQTT.Client("52.72.57.10", Number(1884), "kishan");

image

But i was able to connect to server link using the sample topic