import { Handle, Position } from 'reactflow'; import { FunctionComponent, useEffect } from 'react'; import React, { useState } from 'react'; interface IData { messages: any[] topic: string description: string id: string mqttClient?: any; autoClient?: any; } interface PublishNodeProps { data: IData } export const SubscribeNode: FunctionComponent = ({ data: { topic, description, messages, mqttClient, id, QOS, autoClient } }) => { const [subTopic, setSubTopic] = useState(topic || ''); const [qos, setQos] = useState(QOS || 0); const client = mqttClient || autoClient const handleClick = () => { if (client) { client.subscribe(subTopic, { qos: qos }, function (err) { if (err) { console.error(err); } }); } }; useEffect(() => { if(autoClient){ autoClient.connect() } if (client) { client.on('message', function (topic, message) { console.log(message.toString()) handleAddMessage(message.toString()+ "--/" + topic.toString()) }) } }, []) const [message, setMessage] = useState([]); const handleAddMessage = (mes: string) => { setMessage((prevMessages) => [...prevMessages, mes]); }; const handleDeleteMessage = (index) => { const updatedMessages = [...message]; updatedMessages.splice(index, 1); setMessage(updatedMessages); }; const handleDeleteAllMessages = () => { setMessage([]); }; return (
YOU CAN SUBSCRIBE

{id}

{description && (
{description}
)}

Messages Payloads to expect from listening to this channel
{messages.map((message) => { return (
{message.title}
); })}
setSubTopic(e.target.value)} style={{ appearance: 'none', display: 'block', width: '100%', backgroundColor: '#edf2f7', color: '#4a5568', border: '1px solid #edf2f7', borderRadius: '0.25rem', padding: '0.75rem 1rem', marginBottom: '0.75rem', lineHeight: '1.5', outline: 'none' }} />
Messages Received
{message.length === 0 ? (
No messages
) : ( message.map((message, index) => (
{message}
)) )}
Total Messages: {message.length}
{message.length > 0 && ( )}
); }; export default SubscribeNode;