npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-mqtt-hooks

v0.0.3

Published

React Hooks for MQTT

Downloads

18

Readme

Introduction

React-Mqtt-Hooks is a library that simplifies the integration of MQTT (Message Queuing Telemetry Transport) functionality into React applications. It provides a set of custom hooks that allow developers to easily connect to an MQTT broker based on the popular MQTT.js library.

With these hooks, you can publish messages to specific topics and subscribe to receive messages from the broker. The library seamlessly synchronizes the received MQTT messages with the state of your React functional components, enabling real-time updates and efficient data handling within your application.

✨ Features

  • Global Cache: The message received from the MQTT broker is stored in a global cache, which can be accessed from any component in the application.
  • Real-time Updates: The library automatically updates the state of your components when new messages are received from the broker.

📦 Installation

npm i react-mqtt-hooks
# or
pnpm add react-mqtt-hooks

🚀 Quick Start

  1. First, wrap your application with the MqttConnector component and provide the MQTT Broker URL and the connection options.

    import { MqttConnector } from "react-mqtt-hooks";
    
    function App() {
      return (
        <MqttConnector
          url="ws://example-broker-url/mqtt"
          options={{
            clientId: "your-client-id",
            username: "your-username",
          }}
        >
          {/* Your components */}
        </MqttConnector>
      );
    }
  2. Then, use the useTopic hook to subscribe to a topic and receive messages from the broker in your components within the MqttConnector component.

    import { useTopic } from "react-mqtt-hooks";
    
    function ChatMsg() {
      // This hook return a Buffer object from Broker,
      // you can use toString() to convert it to a string
      // or whatever you want.
      const msg = useTopic("chat");
    
      return (
        <div>
          <h1>Messages from the broker:</h1>
          <p>{msg?.toString()}</p>
        </div>
      );
    }

    useTopic will cache the last message data received from the broker and update the component state under the hood. This concept is inspired by the SWR library.

    Multiple useTopic hook with same topic will share the same message data cache. This means you can call useTopic accross different components and they all will behave the same.

📚 API Refference

MqttConnector

The MqttConnector component is a provider that wraps your application and provides the MQTT.js client instance and connection status to the context. It also handles the connection and disconnection of the client.

import { MqttConnector } from "react-mqtt-hooks";

function App() {
  return (
    <MqttConnector
      url="ws://example-broker-url/mqtt"
      options={{
        clientId: "your-client-id",
      }}
    >
      {/* Your components */}
    </MqttConnector>
  );
}

useMqttConnector

The useMqttConnector hook is used to access MqttConnector component context. It provides 2 values:

  • client: The MQTT.js client instance, more details about the client instance can be found in the MQTT.js documentation.
  • status: The connection status of the client.
import { useMqttConnector } from "react-mqtt-hooks";

function MyComponent() {
  const { client, status } = useMqttConnector();

  return (
    <div>
      <p>Client ID: {client?.options.clientId}</p>
      <p>Status: {status}</p>
    </div>
  );
}

useTopic

This hook currently not support multiple topics and wildcard subscriptions yet.

The useTopic hook is used to subscribe to a specific topic and receive messages from the broker. It returns the last message buffer received from the broker. You can convert it to whatever you want.

import { useTopic } from "react-mqtt-hooks";

function MyComponent() {
  const msg = useTopic("my-topic");

  return <p>{msg?.toString()}</p>;
}