mqtt-react-hooks icon indicating copy to clipboard operation
mqtt-react-hooks copied to clipboard

UseSubscription throws error: "i.off is not a function"

Open japroper opened this issue 3 years ago • 6 comments

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 (

{connectionStatus}
)} ------------------------------------------------------------

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.

japroper avatar Jan 14 '22 15:01 japroper

Same problem.

antoniodinovi avatar Jun 08 '22 07:06 antoniodinovi

Some problem. Do you have a solution for that?

Rampdisli avatar Sep 17 '22 13:09 Rampdisli

You can replace off with removeListener

hi-liang avatar Oct 02 '22 22:10 hi-liang

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 avatar Nov 09 '22 20:11 fractal-joe

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

hi-liang avatar Nov 09 '22 22:11 hi-liang

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

zb-sj avatar Jan 11 '23 06:01 zb-sj