# umi-plugin-mqtt

> A umi plugin that integrates mqtt.js functions

Latest version **1.0.5** (published 2023-06-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install umi-plugin-mqtt
pnpm add umi-plugin-mqtt
yarn add umi-plugin-mqtt
bun add umi-plugin-mqtt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2023-06-06 |
| First published | 2023-04-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tchaos |
| Maintainers | lucksoft |

## Links

- npm: https://www.npmjs.com/package/umi-plugin-mqtt
- npm.io page: https://npm.io/package/umi-plugin-mqtt

## Dependencies (1)

- [mqtt](https://npm.io/package/mqtt.md) ^4.3.7

## Recent versions

- 1.0.5 (latest) — 2023-06-06
- 1.0.4 — 2023-06-06
- 1.0.3 — 2023-04-20
- 1.0.2 — 2023-04-13
- 1.0.1 — 2023-04-07
- 1.0.0 — 2023-04-07

## README

# umi-plugin-mqtt

> 一个集成了[mqtt.js](https://github.com/mqttjs/MQTT.js)功能的umi插件

## Install
```bash
pnpm add umi-plugin-mqtt
```

### 如何开启插件
```js
// .umirc.ts
export default {
  plugins: ['umi-plugin-mqtt'],
  mqtt: {
    host: '10.10.10.153', // mqtt broker主机地址
    port: 32083, // mqtt broker端口
    protocol: 'ws', // 协议
    path: '/mqtt',
  },
}
```

### 运行时配置
```js
// app.ts
export const mqtt = {
  options: {
    username: Cookies.get(KEYS.AUTH_TOKEN),
  },
  topics: ['t/todo', 't/biz'],
  qos: 2,
  onError: (err: any) => {
    console.error('mqtt error: ', err);
  },
};
```

### Example
```js
import React, { useEffect, useCallback } from 'react';
import { useMqtt } from '@umijs/max';

export default ()=> {
  const { client } = useMqtt();

  const handleMessage = useCallback((topic, message) => {
    console.log('handleMessage', topic, message.toString());
  }, []);
  
  useEffect(() => {
    client.subscribe('t/topic1', { qos: 2 });
    client.on('message', handleMessage);
    return () => {
      client.removeListener('message', handleMessage);
      client.unsubscribe('t/topic1');
    };
  }, []);
  
  return <div>MQTT</div>
}
```

---
_Source: https://npm.io/package/umi-plugin-mqtt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
