All Projects → VictorHAS → mqtt-react-hooks

VictorHAS / mqtt-react-hooks

Licence: MIT license
ReactJS library for Pub/Sub communication with an MQTT broker using Hooks

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to mqtt-react-hooks

Ldagibbssampling
Open Source Package for Gibbs Sampling of LDA
Stars: ✭ 218 (+198.63%)
Mutual labels:  topic
angular-PubSub
Angular 1.x implementation of the Publish–Subscribe pattern.
Stars: ✭ 32 (-56.16%)
Mutual labels:  topic
hub
📨 A fast Message/Event Hub using publish/subscribe pattern with support for topics like* rabbitMQ exchanges for Go applications
Stars: ✭ 125 (+71.23%)
Mutual labels:  topic
clustext
Easy, fast clustering of texts
Stars: ✭ 18 (-75.34%)
Mutual labels:  topic
MQTTnet
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
Stars: ✭ 3,309 (+4432.88%)
Mutual labels:  mqtt-broker
odoobooks
Odoo Best Practices - Online Book
Stars: ✭ 46 (-36.99%)
Mutual labels:  topic
Proposal Smart Pipelines
Old archived draft proposal for smart pipelines. Go to the new Hack-pipes proposal at js-choi/proposal-hack-pipes.
Stars: ✭ 177 (+142.47%)
Mutual labels:  topic
smartacus-mqtt-broker
smartacus-mqtt-broker is a Java-based open source MQTT broker that fully supports MQTT 3.x .Using Netty 4.1.37
Stars: ✭ 25 (-65.75%)
Mutual labels:  mqtt-broker
mqttinfo
Attempts to determine the configuration, behavior, and type of a remote MQTT broker
Stars: ✭ 17 (-76.71%)
Mutual labels:  mqtt-broker
hivemq4-docker-images
Official Docker Images for the Enterprise MQTT Broker HiveMQ
Stars: ✭ 18 (-75.34%)
Mutual labels:  mqtt-broker
windowed-observable
Messaging lib using a pub/sub observable scoped by namespaces.
Stars: ✭ 132 (+80.82%)
Mutual labels:  topic
geekforum
基于python3.5、django1.10、xadmin的多用户博客论坛系统
Stars: ✭ 86 (+17.81%)
Mutual labels:  topic
mqtt
The fully compliant, embeddable high-performance Go MQTT v5 server for IoT, smarthome, and pubsub
Stars: ✭ 356 (+387.67%)
Mutual labels:  mqtt-broker
ConDigSum
Code for EMNLP 2021 paper "Topic-Aware Contrastive Learning for Abstractive Dialogue Summarization"
Stars: ✭ 62 (-15.07%)
Mutual labels:  topic
DovakinMQ
MQTT broker for java based on Netty
Stars: ✭ 14 (-80.82%)
Mutual labels:  mqtt-broker
Kafdrop
Kafka Web UI
Stars: ✭ 3,158 (+4226.03%)
Mutual labels:  topic
DForumBundle
Forum Bundle for symfony 3
Stars: ✭ 28 (-61.64%)
Mutual labels:  topic
tydom2mqtt
Link between a Delta Dore's Tydom Hub and and a MQTT broker, unofficial of course.
Stars: ✭ 27 (-63.01%)
Mutual labels:  mqtt-broker
webfunc
Universal Serverless Web Framework. Write Express apps ready to be deployed to Zeit-Now, Google Cloud Functions (incl. functions reacting to Pub/Sub topics or Storage changes), and AWS Lambdas.
Stars: ✭ 71 (-2.74%)
Mutual labels:  topic
mica-mqtt
基于 java aio 实现的低延迟、高性能百万级 mqtt client 组件和 mqtt broker 服务。🔝🔝 记得右上角点个star 关注更新!
Stars: ✭ 128 (+75.34%)
Mutual labels:  mqtt-broker

npm Quality and Build

Overview

This library is focused in help you to connect, publish and subscribe to a Message Queuing Telemetry Transport (MQTT) in ReactJS with the power of React Hooks.

Flow of Data

  1. WiFi or other mobile sensors publish data to an MQTT broker
  2. ReactJS subscribes to the MQTT broker and receives the data using MQTT.js
  3. React's state is updated and the data is passed down to stateless components

Key features

  • React Hooks;
  • Beautiful syntax;
  • Performance focused;

Installation

Just add mqtt-react-hooks to your project:

yarn add mqtt-react-hooks

Hooks availables

  • useMqttState -> return { connectionStatus, client, message }
  • useSubscription(topic: string | string[], options?: {} ) -> return { client, topic, message, connectionStatus }

Usage

Currently, mqtt-react-hooks exports one enhancers. Similarly to react-redux, you'll have to first wrap a root component with a Connector which will initialize the mqtt instance.

Root component

The only property for the connector is the connection information for mqtt.Client#connect

Example Root component:

import React from 'react';

import { Connector } from 'mqtt-react-hooks';
import Status from './Status';

export default function App() {
  return (
    <Connector brokerUrl="wss://test.mosquitto.org:1884">
      <Status />
    </Connector>
  );
}

Example Connection Status

import React from 'react';

import { useMqttState } from 'mqtt-react-hooks';

export default function Status() {
  /*
   * Status list
   * - Offline
   * - Connected
   * - Reconnecting
   * - Closed
   * - Error: printed in console too
   */
  const { connectionStatus } = useMqttState();

  return <h1>{`Status: ${connectionStatus}`}</h1>;
}

Subscribe

Example Posting Messages

MQTT Client is passed on useMqttState and can be used to publish messages via mqtt.Client#publish and don't need Subscribe

import React from 'react';
import { useMqttState } from 'mqtt-react-hooks';

export default function Status() {
  const { client } = useMqttState();

  function handleClick(message) {
    return client.publish('esp32/led', message);
  }

  return (
    <button type="button" onClick={() => handleClick('false')}>
      Disable led
    </button>
  );
}

Example Subscribing and Receiving messages

import React from 'react';

import { useSubscription } from 'mqtt-react-hooks';

export default function Status() {
  /* Message structure:
   *  topic: string
   *  message: string
   */
  const { message } = useSubscription([
    'room/esp32/testing',
    'room/esp32/light',
  ]);

  return (
    <>
      <div style={{ display: 'flex', flexDirection: 'column' }}>
        <span>{`topic:${message.topic} - message: ${message.message}`}</span>
      </div>
    </>
  );
}

Tips

  1. If you need to change the format in which messages will be inserted in message useState, you can pass the option of parserMethod in the Connector:
import React, { useEffect, useState } from 'react';
import { Connector, useSubscription } from 'mqtt-react-hooks';

const Children = () => {
  const { message, connectionStatus } = useSubscription('esp32/testing/#');
  const [messages, setMessages] = useState < any > [];

  useEffect(() => {
    if (message) setMessages((msgs: any) => [...msgs, message]);
  }, [message]);

  return (
    <>
      <span>{connectionStatus}</span>
      <hr />
      <span>{JSON.stringify(messages)}</span>
    </>
  );
};

const App = () => {
  return (
    <Connector
      brokerUrl="wss://test.mosquitto.org:1884"
      parserMethod={msg => msg} // msg is Buffer
    >
      <Children />
    </Connector>
  );
};

Contributing

Thanks for being interested on making this package better. We encourage everyone to help improving this project with some new features, bug fixes and performance issues. Please take a little bit of your time to read our guides, so this process can be faster and easier.

License

MIT ©

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].