npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ko-developerhong/react-native-mqtt

v1.0.2

Published

use mqtt in react-native

Downloads

76

Readme

react-native-mqtt

This library is used to communicate with the message broker using the MQTT protocol. This library is available in a React Native environment and supports TypeScript.

This library uses the following native MQTT client libraries:

iOS - https://github.com/emqx/CocoaMQTT

Android - https://github.com/eclipse/paho.mqtt.android

Installation

To use this library, you must first install it using npm or yarn.

npm install @ko-developerhong/react-native-mqtt
# OR
yarn add @ko-developerhong/react-native-mqtt

IOS Setup

Add Podfile

pod "CocoaMQTT", :modular_headers => true
pod "MqttCocoaAsyncSocket", :modular_headers => true

Import

import MqttClient, { ConnectionOptions, ClientEvent, MQTTEventHandler } from 'react-native-mqtt';

Connect

To connect to the message broker, use the connect method, which takes the host address and connection options as arguments.

import MqttClient, { ConnectionOptions } from 'react-native-mqtt';

const options: ConnectionOptions = {
  clientId: 'myClientId',
  cleanSession: true,
  keepAlive: 60,
  timeout: 60,
  maxInFlightMessages: 1,
  autoReconnect: true,
  username: 'myUsername',
  password: 'myPassword',
  tls: {
    caDer: Buffer.from('myCertificate'),
    cert: 'myCertificate',
    key: 'myKey',
    p12: Buffer.from('myP12'),
    pass: 'myPass',
  },
  allowUntrustedCA: true,
  enableSsl: true,
  protocol: 'mqtts',
};

MqttClient.connect('mqtt://broker.hivemq.com', options)
  .then(() => console.log('Connected'))
  .catch((error) => console.error('Connection failed: ', error));

Subscribe

To subscribe to a specific topic, use the subscribe method.

type MQTT = {
  subscribe: (topic: string, qos?: number) => void;
};
MqttClient.subscribe('myTopic', 0);

Publish a message

To publish a message, use the publish method.

type MQTT = {
  publish: (topic: string, message: string, qos?: number , retained?: boolean) => void,
}
MqttClient.publish('myTopic', 'Hello, World!', 0, false);

Event Handler

This library offers a variety of events. To handle events, use the on, once, and off methods.

const onConnect: MQTTEventHandler<ClientEvent.Connect> = (reconnect) => {
  console.log('Connected', reconnect);
};

MqttClient.on(ClientEvent.Connect, onConnect);

MqttClient.once(ClientEvent.Disconnect, (cause) => {
  console.log('Disconnected', cause);
});

MqttClient.off(ClientEvent.Connect, onConnect);

Disconnect

To disconnect, use the disconnect method.

MqttClient.disconnect();

Close

To close the library, use the close method.

MqttClient.close();

Troubleshooting

The Swift pod CocoaMQTT depends upon MqttCocoaAsyncSocket, which does not define modules.

you may set `use_modular_headers!` globally in your Podfile, or specify `:modular_headers => true` for particular dependencies.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

NOTE

  • This library is available only in React Native environments.
  • This library supports TypeScript.
  • This library is used to communicate with the message broker using the MQTT protocol.
  • This library is based on react-native-mqtt.

License

MIT


Made with create-react-native-library