admin管理员组文章数量:1325079
I need to use publish Subscribe methods in my Ionic 3 application.
I followed this page.
Is there any way we can link MQTT with our Ionic 3 application? If yes, how so? How exactly do I need to go about it for a successful connection?
I installed ng2-mqtt
service using
npm install ng2-mqtt --save
This is my code:
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); });
}
Still I can't get it to work.
Any suggestions and changes will help me. I'm stuck please do.
I need to use publish Subscribe methods in my Ionic 3 application.
I followed this page.
Is there any way we can link MQTT with our Ionic 3 application? If yes, how so? How exactly do I need to go about it for a successful connection?
I installed ng2-mqtt
service using
npm install ng2-mqtt --save
This is my code:
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); });
}
Still I can't get it to work.
Any suggestions and changes will help me. I'm stuck please do.
Share edited Feb 10, 2018 at 15:50 stefanobaghino 12.8k4 gold badges43 silver badges66 bronze badges asked Feb 9, 2018 at 9:27 CodeMineCodeMine 1793 silver badges10 bronze badges1 Answer
Reset to default 6After searching and trying out different things for a while, I found this solution, you can use this library if you want to use MQTT in your project.
Install it using npm install ngx-mqtt --save
Usage: app.module.ts
import { Observable } from 'rxjs/Observable';
import {
IMqttMessage,
MqttModule,
MqttService
} from 'ngx-mqtt';
export const MQTT_SERVICE_OPTIONS = {
hostname: '13.127.53.13',
port: 9001,
path: '/mqtt'
};
export function mqttServiceFactory() {
return new MqttService(MQTT_SERVICE_OPTIONS);
}
@NgModule({
imports: [
BrowserModule,
HttpModule,
MqttModule.forRoot({
provide: MqttService,
useFactory: mqttServiceFactory
}),
IonicModule.forRoot(MyApp)
]
And then you can use it in your page like: (ex: home.ts file)
import { IMqttMessage, MqttModule, MqttService } from 'ngx-mqtt';
import { Observable } from 'rxjs/Observable';
export class HomePage {
constructor( private _mqttService: MqttService)
{
this._mqttService.observe('home/door').subscribe((message: MqttMessage) =>
{
this.sensor1 = message.payload.toString();
console.log(this.sensor1);
});
}
publishMessage()
{
this._mqttService.unsafePublish("home/button", "on", {qos: 0, retain: false});
}
For more information about this library: https://github./sclausen/ngx-mqtt
本文标签: javascriptMQTT Connection with Ionic 3Stack Overflow
版权声明:本文标题:javascript - MQTT Connection with Ionic 3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742146882a2422831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论