tommy44458 / mqtt-vue-hook

Mqtt-Vue-hook is a TypeScript library that makes it easy to connect to an MQTT broker (supports v5) and manages callback functions for various topics.
https://www.npmjs.com/package/mqtt-vue-hook
MIT License
24 stars 3 forks source link

net.createConnection is not a function #8

Open MajorMayer opened 1 year ago

MajorMayer commented 1 year ago

当协议是 mqtt:// 的时候 会出现这个错误 Uncaught (in promise) TypeError: net.createConnection is not a function

tommy44458 commented 1 year ago

Hi @MajorMayer,

It seems that there is no way to connect directly to the broker via TCP on the browser. It would be best if you used WebSocket (ws://, wss://) as your protocol. See here: https://www.hivemq.com/blog/mqtt-essentials-special-mqtt-over-websockets/

jskim82 commented 1 year ago

Hi @tommy44458 I also got the same error image image image

Is it not available in nuxt3?

tommy44458 commented 1 year ago

Hi @jskim82, (sorry that I tagged the wrong name the first time

Maybe you can try another way to connect with your broker:

React or Typescript instance

// src/app.tsx
// protocol = 'wss', 'ws', 'mqtt', ...
// host = ip or domain
// port = 8083, 1883, ...
import { useMQTT } from 'mqtt-vue-hook'
const mqttHook = useMQTT()

mqttHook.connect(`${protocol}://${host}:${port}`, {
    clean: false,
    keepalive: 60,
    clientId: `mqtt_client_${Math.random().toString(16).substring(2, 10)}`,
    connectTimeout: 4000,
})

Also, I think your broker need to open a port for web socket connections. Then, your protocol should be ‘ws’ and port should be ‘your broker’s listening port for web socket’.