Issue with multiple event listeners #935
-
Hi everyone, I have a chat app which is written in React. I've defined multiple hooks which handle different protocols like I have the following code: const useIqStanzaRequest = (callback: (stanza: Stanza) => void) => {
const requestId = useRef<string>();
const onStanzaHandler = (stanza: Stanza) => {
if (stanza.is('iq') && stanza.attrs.id === requestId.current) {
callback(stanza);
}
};
const request = (stanza: Stanza) => {
const id = uuidv4();
const stanzaWithId = { ...stanza };
stanzaWithId.attrs.id = id;
requestId.current = id;
xmpp()?.send(stanzaWithId);
};
useXmppEvent('stanza', onStanzaHandler);
return request;
};
export default useIqStanzaRequest; In const useXmppEvent = (event: string, callback: (...args: any[]) => void) => {
const error = useSelector((state: RootState) => state.xmppConnection.error);
const isClientSet = useSelector((state: RootState) => state.xmppConnection.isClientSet);
useEffect(() => {
if (!error && isClientSet) {
xmpp()?.on(event, callback);
return () => {
xmpp()?.removeListener(event, callback);
};
}
}, [event, callback, error, isClientSet]);
};
export default useXmppEvent; I would really appreciate some help as I'm already stuck on this for quite some time. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
https://github.com/xmppjs/xmpp.js/tree/main/packages/iq#caller |
Beta Was this translation helpful? Give feedback.
https://github.com/xmppjs/xmpp.js/tree/main/packages/iq#caller