mqtt-react-hooks
mqtt-react-hooks copied to clipboard
UseSubscription throws error: "i.off is not a function"
Hello, thanks again for writing this module. I am using nextJS which may be causing part of the behavior issues, or be unrelated. I open a connector in the app.js and I can connect fine and see connection status in another page using:
const { connectionStatus } = useMqttState();
however this throws the error: const { message } = useSubscription('queuesList');
code in skillpage:
import React, { useEffect, useState } from 'react'; import { useMqttState, useSubscription } from 'mqtt-react-hooks';
export default function SkillPage() { const { connectionStatus } = useMqttState(); const { message } = useSubscription('queuesList');
return (
The i.off line of code is in your index.es.js file. Also I noticed if I import client, then do client.subscribe - that works fine still.
Same problem.
Some problem. Do you have a solution for that?
You can replace off
with removeListener
I'm running this with Vite and typescript. I added the following to my vite config resolve: { alias: { mqtt: "mqtt/dist/mqtt.js" } },
Anyone get anywhere with this? @hi-liang what do you mean? can you give an example?
@fractal-joe Sorry this is kinda hazy from a month ago but:
https://github.com/VictorHAS/mqtt-react-hooks/blob/859fbe5f316b8500abb0d59aa84114376ec18978/lib/useSubscription.tsx#L43
off()
is an alias for event emitter method removeListener()
. I forget exactly how or why, but something about how MQTT.js is built for browser target -- maybe it was without events
? -- means that method is missing in the mqtt client class.
I'm using precompiled-mqtt@^4.3.13
personally, and I just changed that one function for my React client and it's fine.
I could just get around by polyfilling it with
import { EventEmitter } from "events";
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
This could be potentially fixed by https://github.com/mqttjs/MQTT.js/pull/1553