usetopic
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

useTopic

Getting started

import React, { useState } from "react";
import useTopic from "usetopic";

const MQTT_SERVER = "ws://test.mosquitto.org:8080";
const TOPIC = "mytopic";

const Feed = () => {
  const { messages, sendMessage } = useTopic(MQTT_SERVER, TOPIC);
  const [value, setValue] = useState("");

  const send = () => {
    sendMessage(value);
    setValue("");
  };

  return (
    <div>
      {messages.map((message, index) => (
        <div key={index}>{message}</div>
      ))}
      <input value={value} onChange={e => setValue(e.target.value)} />
      <button onClick={send}>Send</button>
    </div>
  );
};

Following MQTT.js documentation, Your broker should accept websocket connection in order to use it in a browser.

Documentation

Readme

Keywords

Package Sidebar

Install

npm i usetopic

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

3 kB

Total Files

4

Last publish

Collaborators

  • 5ika