|
1 | 1 |
|
2 | 2 | # React MQTT Workflow Manager |
3 | 3 |
|
4 | | -React MQTT Workflow Manager is a React component library designed to wrap all MQTT pub/sub logic behind the scenes. This manager comunicates with your broker to dispatch actions in your front-end application. The library also was designed to work with [Workflow API Layer](https://github.com/flow-build/workflow-api). |
| 4 | +React MQTT Workflow Manager is a React component library designed to wrap all [MQTT](https://mqtt.org/) sub logic behind the scenes. It deals only with events, no commands. The manager comunicates with your broker to dispatch actions in your front-end application. The library is focused to work with [Workflow API Layer](https://github.com/flow-build/workflow-api). |
5 | 5 |
|
| 6 | +## Table of contents |
6 | 7 |
|
7 | | -## Installation |
| 8 | + - [Installation](#installation) |
| 9 | + - [Dependencies](#dependencies) |
| 10 | + - [Usage](#usage) |
| 11 | + - [Properties](#properties) |
| 12 | + - [WorkflowManagerConfig](#workflowmanagerconfig) |
| 13 | + - [Hooks](#hooks) |
| 14 | + - [Running locally](#running-locally) |
| 15 | + - [Authors](#authors) |
| 16 | + - [License](#license) |
| 17 | + |
| 18 | +## [Installation](installation) |
8 | 19 |
|
9 | 20 | ```bash |
10 | 21 | npm install @flowbuild/react-mqtt-workflow-manager --save |
|
14 | 25 | ```bash |
15 | 26 | yarn add @flowbuild/react-mqtt-workflow-manager |
16 | 27 | ``` |
17 | | -## Dependencies |
| 28 | +## [Dependencies](dependencies) |
18 | 29 |
|
19 | 30 | The `WorkflowManager` component [peer depends](https://docs.npmjs.com/files/package.json#peerdependencies) on the [React](https://www.npmjs.com/package/react) and [React DOM](https://www.npmjs.com/package/react-dom) in version 18. |
20 | 31 |
|
21 | 32 | ```bash |
22 | 33 | npm i react@18 react-dom@18 |
23 | 34 | ``` |
24 | | -## Usage |
| 35 | +## [Usage](usage) |
| 36 | + |
| 37 | + |
| 38 | +1. Before the componente usage, set the store with `WorkflowManagerConfig.setStore`. |
| 39 | + |
| 40 | +```tsx |
| 41 | +// App.tsx |
| 42 | + |
| 43 | +import * as React from 'react'; |
| 44 | + |
| 45 | +import { WorkflowManagerConfig } from '@flowbuild/react-mqtt-workflow-manager'; |
| 46 | + |
| 47 | +import { store } from './store'; // Your redux store |
| 48 | + |
25 | 49 |
|
| 50 | +WorkflowManagerConfig.setStore(store); |
| 51 | + |
| 52 | +// ... |
| 53 | +``` |
26 | 54 |
|
27 | | -1. Wrap you application with ``WorkflowManager`` component. Don't forget to set your store with ``WorkflowManagerConfig.setStore`` before the componente usage. This is important, because the library needs redux functions to work correctly. |
| 55 | +2. Wrap your application with `WorkflowManager`. |
28 | 56 |
|
29 | | -```jsx |
| 57 | +```tsx |
30 | 58 | // App.tsx |
31 | 59 |
|
32 | 60 | import * as React from 'react'; |
@@ -56,59 +84,123 @@ export const App: React.FC = () => { |
56 | 84 | }; |
57 | 85 | ``` |
58 | 86 |
|
59 | | -## Properties |
| 87 | +3. Lastly, set `workflowManagerReducer` to your store reducers. |
| 88 | + |
| 89 | +```ts |
| 90 | +import { configureStore, createSlice } from '@reduxjs/toolkit'; |
| 91 | + |
| 92 | +import { workflowManagerReducer, WorkflowManagerConfig } from '@flowbuild/react-mqtt-workflow-manager'; |
| 93 | + |
| 94 | +const myAppSlice = createSlice({ |
| 95 | + name: '@myApp', |
| 96 | + ... |
| 97 | +}); |
| 98 | + |
| 99 | +export const store = configureStore({ |
| 100 | + reducer: { myApp: myAppSlice.reducer, workflowManagerReducer }, |
| 101 | +}); |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | +## [Properties](properties) |
60 | 106 |
|
61 | 107 | Property | Type | Required | Description |
62 | 108 | --- | --- | --- | --- |
63 | 109 | `brokerUrl` | *string* | false | URL to connect to broker. Use full URL like ws:// |
64 | 110 | `options` | *IClientOptions* | false | MQTT client options. See [MQTT.js](https://github.com/mqttjs/MQTT.js/blob/main/types/lib/client-options.d.ts). |
65 | 111 |
|
| 112 | +## [WorkflowManagerConfig](workflowmanagerconfig) |
| 113 | + |
| 114 | +The library also exposes methods and utilities for your commodity. They can be used out of the context of react components. |
66 | 115 |
|
67 | | -## Running locally |
| 116 | +### setStore(store) |
68 | 117 |
|
69 | | -Clone o projeto |
| 118 | +The library use your redux store to dispatch actions. This is used to dispatch internal actions control and for your applications. |
| 119 | + |
| 120 | +### getMqttClient() |
| 121 | + |
| 122 | +A utility method to be used out of context to react components. Be careful; the method must be able to return `null` if an error happens when setting connect. [See here](https://github.com/mqttjs/MQTT.js/blob/main/README.md#client). |
| 123 | + |
| 124 | +### subscribe(topic/topic array/topic object, [options]) |
| 125 | + |
| 126 | +Works like exactly like [mqtt#subscribe](https://github.com/mqttjs/MQTT.js/blob/main/README.md#mqttclientsubscribetopictopic-arraytopic-object-options-callback), but the library implements validations and internal rules. |
| 127 | + |
| 128 | +### subscribe(topic/topic array/topic object, [options]) |
| 129 | + |
| 130 | +Works like exactly like [mqtt#unsubscribe](https://github.com/mqttjs/MQTT.js/blob/main/README.md#mqttclientunsubscribetopictopic-array-options-callback), but the library implements validations and internal rules. |
| 131 | + |
| 132 | +## [Hooks](hooks) |
| 133 | + |
| 134 | +Some hooks are exported for commodity. |
| 135 | + |
| 136 | +### useMqtt() |
| 137 | + |
| 138 | +The hook returns a object contaning `client`, `status` and `error`. |
| 139 | + |
| 140 | +Property | Type | Default value | Description |
| 141 | +--- | --- | --- | --- |
| 142 | +`client` | *MqttClient* | `null` | See type [here](https://github.com/mqttjs/MQTT.js/blob/main/types/lib/client.d.ts) |
| 143 | +`status` | *string* | `offline` | `connecting`, `connected`, `disconnected`, `reconnecting`, `offline` or `error`. |
| 144 | +`error` | *Error* | `null` | |
| 145 | + |
| 146 | +### useSubscribe() |
| 147 | + |
| 148 | +Returns `WorkflowManagerConfig.subscribe` for your commodity. |
| 149 | + |
| 150 | +### UseUnsubscribe() |
| 151 | + |
| 152 | +Returns `WorkflowManagerConfig.unsubscribe` for your commodity. |
| 153 | + |
| 154 | +## [Running locally](running-locally) |
| 155 | + |
| 156 | +Clone the project |
70 | 157 |
|
71 | 158 | ```bash |
72 | 159 | git clone https://github.com/flow-build/react-mqtt-workflow-manager.git |
73 | 160 | ``` |
74 | 161 |
|
75 | | -Entre no diretório do projeto |
| 162 | +Go to the project directory |
76 | 163 |
|
77 | 164 | ```bash |
78 | 165 | cd react-mqtt-workflow-manager |
79 | 166 | ``` |
80 | 167 |
|
81 | | -Instale as dependências |
| 168 | +Install dependencies |
82 | 169 |
|
83 | 170 | ```bash |
84 | 171 | npm install |
85 | 172 | ``` |
86 | 173 |
|
87 | | -Inicie o servidor |
| 174 | +Start the development server |
88 | 175 |
|
89 | 176 | ```bash |
90 | 177 | npm run dev |
91 | 178 | ``` |
92 | 179 |
|
| 180 | +Go to the project example directory |
| 181 | + |
93 | 182 | ```bash |
94 | 183 | cd app |
95 | 184 | ``` |
96 | 185 |
|
| 186 | +Install de example dependencies |
| 187 | + |
97 | 188 | ```bash |
98 | 189 | npm install |
99 | 190 | ``` |
100 | 191 |
|
| 192 | +Start the example application |
| 193 | + |
101 | 194 | ```bash |
102 | 195 | npm run start |
103 | 196 | ``` |
104 | 197 |
|
105 | 198 |
|
106 | | -## Authors |
| 199 | +## [Authors](authors) |
107 | 200 |
|
108 | 201 | [@wallace-sf](https://www.github.com/wallace-sf) |
109 | 202 |
|
110 | 203 |
|
111 | | -## License |
| 204 | +## [License](license) |
112 | 205 |
|
113 | 206 | [MIT](https://choosealicense.com/licenses/mit/) |
114 | | - |
|
0 commit comments