Skip to content

Commit e31eb49

Browse files
committed
feat: add custom parser for incoming mqtt message
1 parent e76bfd4 commit e31eb49

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

.changeset/eleven-points-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-mqtt-hooks": minor
3+
---
4+
5+
feat: add custom parser for incoming mqtt message

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pnpm add react-mqtt-hooks mqtt
7070

7171
### `MqttConnector`
7272

73-
The `MqttConnector` component is a provider that wraps your application and provides the raw `MqttClient` instance from [MQTT.js](https://github.com/mqttjs/MQTT.js) to the context. It also handles the connection and disconnection of the client.
73+
The `MqttConnector` component is a provider that wraps your application and provides the raw `MqttClient` instance from [MQTT.js](https://github.com/mqttjs/MQTT.js?tab=readme-ov-file#client) to the context. It also handles the connection and disconnection of the client.
7474

7575
All hooks provided by this library must be used within the `MqttConnector` component.
7676

src/contexts/mqtt-connector.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22
import type { IClientOptions } from "mqtt";
3+
import type { Buffer } from "node:buffer";
34
import type { MqttClientType } from "./mqtt-context";
45
import mqtt from "mqtt";
56
import { useEffect, useState } from "react";
@@ -10,23 +11,25 @@ type MqttConnectorProps = {
1011
children: React.ReactNode;
1112
url: string;
1213
options?: IClientOptions;
14+
customParser?: (message: Buffer) => any;
1315
};
1416

15-
export function MqttConnector({ children, url, options }: MqttConnectorProps) {
17+
export function MqttConnector({ children, url, options, customParser }: MqttConnectorProps) {
1618
const [mqttClient, setMqttClient] = useState<MqttClientType>(null);
1719
const cache = MqttCache.getInstance();
1820
useEffect(() => {
1921
const client = mqtt.connect(url, options);
2022
setMqttClient(client);
2123
client.on("connect", () => {
2224
cache.setClient(client);
25+
cache.setCustomParser(customParser);
2326
});
2427

2528
return () => {
2629
cache.setClient(null);
2730
client.end();
2831
};
29-
}, [url, options, cache]);
32+
}, [url, options, cache, customParser]);
3033
return (
3134
// disable this rule to make it compatible with React 18
3235
// eslint-disable-next-line react/no-context-provider

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ import { MqttConnector } from "./contexts/mqtt-connector";
33
import useMqttClient from "./hooks/use-mqtt-client";
44
import useTopic from "./hooks/use-topic";
55
import useTopics from "./hooks/use-topics";
6-
// import { MqttCache } from "./internals/mqtt-cache";
76

87
export { MqttConnector, useMqttClient, useTopic, useTopics };

src/internals/mqtt-cache.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ export type CacheItem<T> = {
1111
// Global cache for MQTT messages
1212
export class MqttCache {
1313
private static instance: MqttCache;
14-
cache = new Map<string, CacheItem<any>>();
15-
private mqttClient: MqttClient | null = null;
14+
private cache = new Map<string, CacheItem<any>>();
1615
private observers: Map<string, Set<Function>> = new Map();
16+
private mqttClient: MqttClient | null = null;
17+
private customParser: ((message: Buffer) => any) | undefined = undefined;
1718

1819
static getInstance(): MqttCache {
1920
if (!MqttCache.instance) {
@@ -29,12 +30,16 @@ export class MqttCache {
2930
}
3031
}
3132

33+
setCustomParser(parser: ((message: Buffer) => any) | undefined) {
34+
this.customParser = parser;
35+
}
36+
3237
private setupMessageListener() {
3338
if (!this.mqttClient) {
3439
return;
3540
}
3641
this.mqttClient.on("message", (topic: string, message: Buffer) => {
37-
const parsedMsg = parseMessage(message);
42+
const parsedMsg = this.customParser ? this.customParser(message) : parseMessage(message);
3843
this.setData(topic, parsedMsg);
3944
this.notifyObservers(topic, parsedMsg);
4045
});

0 commit comments

Comments
 (0)