mqtt-vue-hook icon indicating copy to clipboard operation
mqtt-vue-hook copied to clipboard

net.createConnection is not a function

Open MajorMayer opened this issue 2 years ago • 3 comments

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

MajorMayer avatar Dec 10 '22 11:12 MajorMayer

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/

tommy44458 avatar Dec 12 '22 10:12 tommy44458

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

Is it not available in nuxt3?

jskim82 avatar Feb 10 '23 01:02 jskim82

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’.

tommy44458 avatar Feb 10 '23 04:02 tommy44458