No matter what i do i can't connect to a mqtt broker via websocket in my angular application (trying in chrome and firefox).
For simplicity i'm using HiveMQ broker, i've published on the topic /gat/38/openReservationRequests some data
I've followed this medium article on how to connect to mqtt in angular using ngx-mqtt but for me it is not working.
In my app:
I've installed the module
npm install ngx-mqtt --save
i've added the configuration and set the module forRoot in my app.module.ts
...
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = {
connectOnCreate: true,
hostname: 'broker.hivemq.com',
port: 8000,
path: '/gat/38/openReservationRequests',
protocol: 'ws',
};
...
imports: [
...
MqttModule.forRoot(MQTT_SERVICE_OPTIONS),
...
],
...
i'm executing this function inside the ngOnInit of app.component.ts
...
import { IMqttMessage, MqttConnectionState, MqttService } from 'ngx-mqtt';
...
constructor(private mqttService: MqttService) {
this.mqttService.state.subscribe((s: MqttConnectionState) => {
const status = s === MqttConnectionState.CONNECTED ? 'CONNECTED' : 'DISCONNECTED';
this.status.push(`Mqtt client connection status: ${status}`);
});
}
ngOnInit() {
this.subscription = this.mqttService
.observe('/gat/38/openReservationRequests')
.subscribe((message: IMqttMessage) => {
this.msg = message;
console.log('msg: ', message);
console.log('Message: ' + message.payload.toString() + 'for topic: ' + message.topic);
console.log('subscribed to topic: ' + /gat/38/openReservationRequests);
});
}
but i am always getting this error:
core.js:6014 ERROR TypeError: Cannot read property 'resubscribe' of undefined
at MqttClient.subscribe (mqtt.min.js:1)
at mqtt.service.js:211
at Observable._subscribe (using.js:8)
at Observable._trySubscribe (Observable.js:42)
at Observable.subscribe (Observable.js:28)
at FilterOperator.call (filter.js:13)
at Observable.subscribe (Observable.js:23)
at Observable.connect (ConnectableObservable.js:30)
at RefCountOperator.call (refCount.js:17)
at Observable.subscribe (Observable.js:23)
mqtt.min.js:1 WebSocket connection to 'ws://broker.hivemq.com:8000/gat/38/openReservationRequests' failed: Connection closed before receiving a handshake response
if i specify the clientId inside the MQTT_SERVICE_OPTIONS i still get the same error.
if i change the protocol to wss i get a different error:
core.js:6014 ERROR TypeError: Cannot read property 'resubscribe' of undefined
at MqttClient.subscribe (mqtt.min.js:1)
at mqtt.service.js:211
at Observable._subscribe (using.js:8)
at Observable._trySubscribe (Observable.js:42)
at Observable.subscribe (Observable.js:28)
at FilterOperator.call (filter.js:13)
at Observable.subscribe (Observable.js:23)
at Observable.connect (ConnectableObservable.js:30)
at RefCountOperator.call (refCount.js:17)
at Observable.subscribe (Observable.js:23)
mqtt.min.js:1 WebSocket connection to 'wss://broker.hivemq.com:8000/gat/38/openReservationRequests' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED
If i try to connect manually inside my app.component.ts ngOnInit before observing the topic:
this.mqttService.connect({
hostname: 'broker.hivemq.com',
port: 8000,
path: '/gat/38/openReservationRequests',
clientId: '34er23qwrfq42w3' //those are just random digits
});
i still get the error above.
For me it would be ideal to connect in some inner component (accessible after the user is authenticated) because i will have my private mqtt broker and the topic will depend on the logged user information.
I've tried any combination of protocol with/without cliendId etc but at this point i don't know what is wrong. I've already fully recompiled my app lots of times, i've tried publishing it on my test-server which has a ssl certificate but nothing changed.
Resolved thanks to #Anant Lalchandani i set the correct path.
The other problem was that '/mytopic' and 'mytopic' are indeed two different topic and i was using it wrong too.
This is my code, updated:
app.module.ts
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = {
connectOnCreate: false,
hostname: 'broker.hivemq.com',
port: 8000,
path: '/mqtt'
};
appcomponent.ts (inside ngOnInit for now)
this.mqttService.connect({
hostname: 'broker.hivemq.com',
port: 8000,
path: '/mqtt',
clientId: '1234e3qer23rf'
});
this.mqttService.onConnect
.subscribe(
connack=> {
console.log('CONNECTED');
console.log(connack);
}
);
this.mqttService.observe('gat/38/openReservationRequests')
.subscribe((message: IMqttMessage) => {
this.msg = message;
console.log(new TextDecoder('utf-8').decode(message.payload));
});
I have checked the code snippets you shared in question.
In your app.module.ts, the path value should be '/mqtt'. You have set the topic as the value of path here. The topic can only be subscribed/published. As you are using a topic as a path value at the time of connecting to a websocket, your application will not be able to connect to websocket at the first place.
The reason why we need to use /mqtt as a path is it specifies you are sending MQTT messages over the WebSocket protocol.
The documentation of HiveMQ itself stated to use the path as '/mqtt' in its example. You can check the documentation here.
Related
I can't implement any subscriptions because it suddenly disconnects from it when I try to listen to some endpoint with GraphQL Playground:
{"error": "Could not connect to websocket endpoint wss://localhost:4000/graphql. Please check if the endpoint url is correct."}
I'm using ApolloServer alone, no express or anything else. It is containerized with Docker using node14 image, the port is properly fowarded, queries and mutations works properly.
This is the configuration snippet:
const server = new ApolloServer({
typeDefs: mergedTypeDefs,
resolvers: mergedResolvers,
playground: {
subscriptionEndpoint: 'ws://localhost:4000/graphql'
},
subscriptions: {
keepAlive: 9000,
onConnect: (connParams, webSocket, context) => {
console.log('CLIENT CONNECTED');
},
onDisconnect: (webSocket, context) => {
console.log('CLIENT DISCONNECTED')
}
},
context: {
models
}
});
I tried everything, from using 'wss' instead of 'ws' to change the path. I checked for typos and didn't find one. What bothers me is that the paths are the same so It should at least try to notify me by the onConnect or onDisconnect but it doesn't.
This is the message through Chrome's dev tools:
WebSocket connection to 'wss://localhost:4000/graphql' failed: WebSocket is closed before the connection is established.
I tested subscriptions with a 'tutorial' project outside the container and it works fine.
Sometimes, the only function of subscriptions that works is onDisconnect but after 2-5 seconds after receiving the error message on PlayGround, and Still it doesn't gives me any insight on the problem.
Any help or suggestion is appreciated.
wss will definitely not work locally without certificate, so use ws as a protocol.
If it works without Docker, then everything should be fine code-wise.
You should also make sure that you've mapped ports correctly, i.e. exposed port 4000 https://docs.docker.com/engine/reference/commandline/run/#publish-or-expose-port--p---expose
I have installed ThingsBoard server on one PC (UBUNTU16.04) and ThingsBoard Gateway on another PC(UBUNTU18.04) ,In order to send data to ThingsBoard Gateway I installed Mosquitto MQTT broker on another PC.I followed configuration guides to connect broker to Gateway as well as server (using access token and host ip).
I connected temperature sensor to ESP32. While I am trying to send the data to gateway through MQTT the data is not getting to the gateway.The topic I used here is "v1/gateway/telemetry" in order to publish the data.
Can we use Gateway Device ID to send data?
How can I send data either by using topic or by using device id or by using device access token?(from device)
All the PC 's are connected to the same network(Private network).
I am facing this issue can Someone please sort it out...
You need to create a Proxy Layer Between MQTT Broker and your server.
var mqtt = require('mqtt'), url = require('url');
var client = mqtt.connect('mqtt://localhost:1883',
{
username: '<username>',
password: '<password>'
});
console.log("Connected to MQTT Broker:- localhostā + client.toString());
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: Certificate key file path,
certPath: Certificate file path,
caPath: Certificate root file path,
clientId: AWS Thing Name,
region: AWS IoT Broker region,
});
device.on('connect', function ()
{
console.log("Connected to AWS IoT Broker:- " + device.toString());
});
client.on('connect', function()
{
//subscribe to a topic (#)
client.subscribe('#', function ()
{
client.on('message', function (topic, message, packet) {
console.log("Received :-" + message + " on " + topic);
device.publish(topic, message);
console.log("Sent :-" + message + " on " + topic);
});
});
});
Something like this might help you.
I am having this code in my file (belong to react)
const client = mqtt.connect({
host: 'mqtt://m16.cloudmqtt.com',
port: 1883,
username: 'b*******k',
password: 'gU******S',
});
client.on('connect', () => {
console.log('hello');
client.subscribe('v');
client.publish('v', 'chal pa');
});
client.on('message', (topic, message) => {
if (topic === 'v') {
console.log('here my topic is v');
// var connected = (message.toString() === 'true');
}
console.log('recived message from mqtt');
console.log(message);
});
client.on('error', er => {
console.log(er);
});
I am expecting to connect to mqtt broker and receive some message.
But nothing happened. When i check log file in cloudmqtt.com
I am stuck here can anybody help. Link to any blog/video that will help will be highly appreciated.
I am using mqttjs
You have explicitly told the MQTTjs library to use native MQTT rather than MQTT over Websockets by using mqtt:// on the start of the URI.
If you want to use MQTT over websockets the URI should start with ws://
Secondly you are using port 1883, this is normally used for native MQTT not MQTT over websockets. The cloudmqtt docs suggest you should be using a port number that starts with a 3 to access the websockets listener.
I'm trying a react native application using couchDB 2.1.1. PouchDB entry in package json looks like this:
"pouchdb": "^6.3.4",
"pouchdb-react-native": "^6.3.4",
Replication is as shown below:
const localDB = new PouchDB('employee');
const remoteDB = new PouchDB('http://username:password#localhost:5984/employee');
localDB.replicate.from(
remoteDB,
(err, response) => {
if (err) {
return console.log(err);
}
},
);
I get following error:
{"code":"ETIMEDOUT","status":0,"result":{"ok":false,"start_time":"...","docs_read":0,"docs_written":0,"doc_write_failures":0,"errors":[],"status":"aborting","end_time":"...","last_seq":0}}
Almost all the times this works fine when I run the app in debug mode. Tried ajax timeout as shown here PouchDB ETIMEDOUT error. This didn't work. Is there something that I'm supposed to look in my code? Please help.
Had the same issue, the following fixed it for me:
Use your PC ip address instead of localhost
Configure your firewall
to allow connections on port 5984 OR just disable it (Not
recommended)
I am new in MQTT so can someone help me for connecting MQTT with Mosquitto using javascript i am using this code but it give error...
Connection failed: AMQJS0007E Socket error:undefined.
My Code is :
<script type='text/javascript' src='jquery-1.10.1.js'></script>
<script type='text/javascript' src="mqttws31.js"></script>
var client = new Messaging.Client("ns.testingindia.tld", 1883, "myclientid_" + parseInt(Math.random() * 100, 10));
//Gets called if the websocket/mqtt connection gets disconnected for any reason
client.onConnectionLost = function (responseObject) {
//Depending on your scenario you could implement a reconnect logic here
alert("connection lost: " + responseObject.errorMessage);
};
//Gets called whenever you receive a message for your subscriptions
client.onMessageArrived = function (message) {
//Do something with the push message you received
$('#messages').append('Topic: ' + message.destinationName + ' | ' + message.payloadString + '');
};
//Connect Options
var options = {
timeout: 3,
//Gets Called if the connection has sucessfully been established
onSuccess: function () {
alert("Connected");
},
//Gets Called if the connection could not be established
onFailure: function (message) {
document.write("Connection failed: " + message.errorMessage);
alert("Connection failed: " + message.errorMessage);
}
};
//Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
var publish = function (payload, topic, qos) {
//Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
var message = new Messaging.Message(payload);
message.destinationName = topic;
message.qos = qos;
client.send(message);
}
//]]>
You are connecting to port 1883 which is the default MQTT port. I assume you mean to use Websockets, and that would typically be configured on a different port number. If the broker you're using has Websocket support, ensure you connect to the correct port with Messaging.Client().
If you're using the Mosquitto broker, you'll need version 1.4 from its bitbucket repository for Websocket support, but note that Mosquitto 1.4 hasn't yet been released.
A quick way to test that your broker isn't causing the problem is to connect to broker.mqttdashboard.com port:8000 if that doesn't work my next guess is that you have just mosquitto installed and no websockets server, which you need if you want to use JS to connect directly to the broker over the web.
Another, but quicker way to get up and running now is downloading hivemq (trial version supports 25 connections) it has a mqtt broker with websockets built in and will run on windows and will be up and running in 5 mins.
Which version of Mosquitto are you using?
The current release version (1.3.4) does not natively support Websockets (next version will)
You can use something like lighttpd with mod_websockets to supply websocket support (instructions for linux are linked to from here: http://test.mosquitto.org/ws.html) or you can build a new version of Mosquitto from the head of the source tree