I am facing a challenge with implementing Publish-Subscribe methods in my Ionic 3 application.
After consulting the information on this page, I attempted to link MQTT with my Ionic 3 application.
Can anyone guide me on how to successfully connect MQTT with an Ionic 3 application? What steps should I follow for a seamless connection?
To start, I installed the ng2-mqtt
service by running the following command:
npm install ng2-mqtt --save
Below are snippets of the code I have written:
index.html
<script src="cordova.js"></script>
<script src="node_modules/ng2-mqtt/mqttws31.js" type="text/javascript"></script>
home.ts
import {Paho} from 'mqttws31'
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private _client: Paho.MQTT.Client;
constructor(public paho: Paho) {
}
this._client = new Paho.MQTT.Client("52.66.30.178", 1883, "path", "someclient_id");
this._client.onConnectionLost = (responseObject: Object) => {
console.log('Connection lost.');
this.getServerMessage();
this._client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived.');
};
this._client.connect({ onSuccess: this.onConnected.bind(this); });
}
Despite following this setup, I am still experiencing issues with the implementation.
I would greatly appreciate any suggestions or changes that can help me overcome this challenge. I am currently stuck and seeking assistance.