After 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,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}); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。