Я пробовал так много вещей, но единственная работа для меня - ниже (Angular 8)
установите следующую команду в вашем проекте
npm в буфере - сохранить
npm i -S process
npm i mqtt --save
Добавить следующую строку в polyfills.js в конце
(window as any)['global'] = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
import * as process from 'process';
window['process'] = process;
добавьте следующий код в app.component.ts
import { Component } from '@angular/core';
import * as mqttt from "mqtt";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'checkMqttFromWeb';
constructor(){
var client = mqttt.connect('mqtt://192.168.0.29:1884')
client.on('connect', function () {
client.subscribe('fooBar', function (err) {
if (!err) {
client.publish('fooBar', 'Hello mqtt')
}
})
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})
}
}
Запустите ваше приложение, используя ng serve
надеюсь, что это работает