MQTT client for React Native application.
- Secure MQTT connection over TLS 1.2.
- Authentication of both of server and client by X.509 certificates.
- Certificates and a private key stored in a device specific key store.
- Android KeyStore on Android
- Default keychain on iOS
This library wraps the following libraries,
-
Paho MQTT Client for Android variant maintained by hannesa2 (Android)
This library is forked as emoto-kc-ak/paho.mqtt.android to make Maven artifacts.
-
CocoaMQTT (iOS)
npm install --save git+https://github.com/emoto-kc-ak/react-native-mqtt-client.git#v0.1.4
import MqttClient from 'react-native-mqtt-client';
You have to configure an identity before connecting to an MQTT broker.
MqttClient.setIdentity
configures an identity to communicate with an MQTT broker.
Certificates and a private key are stored in a device specific key store.
MqttClient.setIdentity({
caCertPem: IOT_CA_CERT, // PEM representation string of a root certificate
certPem: IOT_CERT, // PEM representation string of a client certificate
keyTag: IOT_KEY, // key tag of the private key in Keystore/Keychain
keyStoreOptions, // options for a device specific key store. may be omitted
})
.then(() => {
/* handle success */
})
.catch(({ code, message }) => {
/* handle error */
});
keyStoreOptions
is an optional object that may have the following fields,
caCertAlias
: (string) Alias associated with a root certificate (Android only). SeeKeyStore.setCertificateEntry
keyAlias
: (string) Alias associated with a private key (Android only). SeeKeyStore.setKeyEntry
caCertLabel
: (string) Label associated with a root certificate (iOS only). SeekSecAttrLabel
certLabel
: (string) Label associated with a client certificate (iOS only). SeekSecAttrLabel
keyApplicationTag
: (string) Tag associated with a private key (iOS only). SeekSecAttrApplicationTag
MqttClient.connect
connects to an MQTT broker.
MqttClient.connect({
host: IOT_ENDPOINT, // (string) Host name of an MQTT broker to connect.
port: IOT_PORT, // (number) Port to connect.
clientId: IOT_DEVICE_ID, // (string) Client ID of a device connecting.
})
.then(() => {
/* handle success */
})
.catch(({ code, message }) => {
/* handle error */
});
It attempts to connect to ssl://$IOT_ENDPOINT:$IOT_PORT
.
MqttClient.publish
publishes a message to an MQTT broker.
MqttClient.publish(topic, payload)
.then(() => {
/* handle success */
})
.catch(({ code, message }) => {
/* handle error */
});
Where,
topic
: (string) Topic wherepayload
is to be published.payload
: (string) Payload to be published. Usually a stringified JSON object.
MqttClient.subscribe
subscribes a topic of an MQTT broker.
MqttClient.subscribe(topic)
.then(() => {
/* handle success */
})
.catch(({ code, message }) => {
/* handle error */
});
Where,
topic
: (string) Topic to subscribe.
To handle messages in the subscribed topic, you have to handle a receive-message
event.
MqttClient.disconnect
disconnects from an MQTT broker.
MqttClient.disconnect();
MqttClient.isConnected
checks if client is connected to an MQTT Broker.
MqttClient.isConnected();
An identity stored in a device specific key store by MqttClient.setIdentity
may be loaded by MqttClient.loadIdentity
.
MqttClient.loadIdentity(keyStoreOptions)
.then(() => {
/* handle success */
})
.catch(({ code, message }) => {
/* handle error */
});
Please refer to Configuring an identity for details of keyStoreOptions
.
An identity stored in a device specific key store by MqttClient.setIdentity
may be cleared by MqttClient.resetIdentity
.
MqttClient.resetIdentity(keyStoreOptions)
.then(() => {
/* handle success */
})
.catch(({ code, message }) => {
/* handle error */
});
Please refer to Configuring an identity for details of keyStoreOptions
.
MqttClient.isIdentityStored
tests if an identity is stored in a device-specific key store.
MqttClient.isIdentityStored(keyStoreOptions)
.then((isStored) => {
/* handle success */
})
.catch(({ code, message }) => {
/* handle error */
});
Where,
isStored
: (boolean) Whether an identity is stored in a device-specific key store.
Please refer to Configuring an identity for details of keyStoreOptions
.
MqttClient
emits events when its state is changed, a message is arrived, and an error has occurred.
A connected
event is notified when connection to an MQTT broker is established.
MqttClient.addListener('connected', () => {
/* handle connection */
});
A disconnected
event is notified when connection to an MQTT broker is disconnected.
MqttClient.addListener('disconnected', () => {
/* handle disconnection */
});
A received-message
event is notified when a message is arrived from an MQTT broker.
MqttClient.addListener('received-message', ({ topic, payload }) => {
/* handle message */
});
Where,
topic
: (string) Topic where a message has been published.payload
: (string) Payload of a message. Usually a stringified JSON object.
A got-error
event is notified when an error has occurred.
MqttClient.addListener('got-error', (err) => {
/* handle error */
});
You may face an error similar to the following, when you run pod install
.
[!] The following Swift pods cannot yet be integrated as static libraries:
The Swift pod `CocoaMQTT` depends upon `CocoaAsyncSocket`, which does not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set `use_modular_headers!` globally in your Podfile, or specify `:modular_headers => true` for particular dependencies.
In my case (React Native v0.63.3, CocoaPod v1.9.3), this error was solved by adding the following line to the Podfile
of my application.
pod 'CocoaAsyncSocket', :modular_headers => true
To package this library, please run the following command.
npm run prepare
Artifacts will be updated in the following directories,
lib/commonjs
lib/module
lib/typescript
Sorry, the example
directory is not maintained so far.
MIT
This project is bootstrapped with callstack/react-native-builder-bob.