Have ideas to improve npm?Join in the discussion! »

@pubgcorp/mqtt-handler
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

mqtt-handler

Basic Usage

in case of instantiate MqttHandler

import MqttHandler from 'mqtt-handler';
 
const mqttHandler = new MqttHandler(
  [
    "my-mqtt-endpoint-1",
    "my-mqtt-endpoint-2",
  ],
  "my-mqtt-username",
  "my-mqtt-password",
);

in case of publish messages

await mqttHandler.connect();
 
await mqttHandler.publishMessage('my-topic', {
'my-key': 'my-value',
});
 
await mqttHandler.end();

in case of receive messages

await mqttHandler.connect({
  'my-topic-1': async (message) => {
    // do something with messages received from topic "my-topic-1"
  },
  'my-topic-2': async (message) => {
    // do something with messages received from topic "my-topic-2"
  },
});

in case of add onMessage handlers or remove onMessage handlers

await mqttHandler.addOnMessageHandler('my-topic-3', async (message) => {
    // do something with messages received from topic "my-topic-3"
});
 
await mqttHandler.removeOnMessageHandler('my-topic-3');

in case of publish http request and response

// client side
import ClientSideMqttHandler, { MqttRequestMessage } from 'mqtt-handler';
 
const clientSideMqttHandler = new ClientSideMqttHandler(
  [
    "my-mqtt-endpoint-1",
    "my-mqtt-endpoint-2",
  ],
  "my-mqtt-username",
  "my-mqtt-password",
);
 
await clientSideMqttHandler.connect();
 
const requestMessage: MqttRequestMessage = {
  url: 'my-url',
  method: 'POST',
  data: {
    message: 'message sent.'
  },
};
 
const response = await clientSideMqttHandler.publishRequestMessage('my-request-topic', requestMessage);
 
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
 
await clientSideMqttHandler.end();
 
// server side
import ServerSideMqttHandler, { MqttResponseMessage } from 'mqtt-handler';
 
const handler = async message => {
  const { mqttRequestMessage, responseTopic } = JSON.parse(message);
 
  const { url, method, headers, data } = mqttRequestMessage;
  
  const responseMessage: MqttResponseMessage = {
    data: {
      message: `message received, url: ${url}, method: ${method}, headers: ${JSON.stringify(headers)}, data: ${JSON.stringify(data)}.`,
    },
    status: 200,
    statusText: 'OK',
  };
    
  await serverSideMqttHandler.publishResponseMessage(responseTopic, responseMessage);
}
 
await serverSideMqttHandler.connect({
  'my-request-topic': handler,
});

in case of handle request timeout

// client side
import ClientSideMqttHandler, { MqttRequestMessage } from 'mqtt-handler';
 
const clientSideMqttHandler = new ClientSideMqttHandler(
  [
    "my-mqtt-endpoint-1",
    "my-mqtt-endpoint-2",
  ],
  "my-mqtt-username",
  "my-mqtt-password",
);
 
await clientSideMqttHandler.connect();
 
const requestMessage: MqttRequestMessage = {
  url: 'my-url',
  method: 'POST',
  data: {
    message: 'message sent.'
  }
};
 
const timeout = 1000;
 
try {
  const response = await clientSideMqttHandler.publishRequestMessage('my-request-topic', requestMessage, timeout)
} catch (error) {
  if (error.message === 'Request Timeout') {
    // do something in case of request timeout
  }
}

Keywords

none

Install

npm i @pubgcorp/mqtt-handler

DownloadsWeekly Downloads

1

Version

0.1.8

License

Apache-2.0

Unpacked Size

44 kB

Total Files

36

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar