rnmrtt-client
TypeScript icon, indicating that this package has built-in type declarations

1.0.13 • Public • Published

rnmrtt-client

A TypeScript-based MQTT client module for React Native with support for JWT authentication. This package enables easy connection, subscription, message publishing, and disconnection with MQTT brokers using WebSockets.

Features

  • WebSocket Support: Connect to MQTT brokers over WebSocket.
  • JWT Authentication: Use JWT tokens for secure authentication.
  • Flexible: Offers essential MQTT functionalities tailored for React Native.

Installation

To install rnmrtt-client:

npm install rnmrtt-client

Usage

Here's a quick example of how to use the rnmrtt-client module in a TypeScript-based React Native project.

import MQTTClient from 'rnmrtt-client';

const brokerUrl = 'ws://your-broker-url.com:8083/mqtt';
const options = {
  clientId: 'YOUR_CLIENT_ID',            // Unique client ID for the connection
  username: 'YOUR_USER_NAME',           // MQTT username
  password: 'YOUR_JWT_TOKEN',          // JWT token as password
  protocol: 'ws' as 'ws',              // Specify WebSocket protocol
  protocolVersion: 4 as 3 | 4 | 5,     // MQTT protocol version
};

// Connect to the MQTT broker
MQTTClient.connect(brokerUrl, options);

// Subscribe to a topic
MQTTClient.subscribe('TOPIC_NAME');

// Publish a message to the topic
MQTTClient.publish('TOPIC_NAME', 'Hello from React Native!');

// Set up a listener for incoming messages
MQTTClient.onMessage((topic, message) => {
  console.log(`Received message on ${topic}: ${message}`);
});

// Disconnect after 20 seconds (for demonstration)
setTimeout(() => {
  MQTTClient.disconnect();
}, 20000);

API Reference

1. connect(brokerUrl: string, options: MQTTClientOptions)

Establishes a connection to the MQTT broker.

  • Parameters:

    • brokerUrl: The URL of the MQTT broker (e.g., ws://broker.example.com:8083/mqtt).
    • options: An object for MQTT connection options.
      • clientId: Unique client ID for the connection.
      • username: MQTT broker username.
      • password: JWT token as password for authentication.
      • protocol: WebSocket protocol ('ws' or 'wss').
      • protocolVersion: MQTT protocol version (3, 4, or 5).
      • keepalive: Keepalive interval in seconds (default: 60).
      • reconnectPeriod: Reconnection interval in milliseconds (default: 1000).
  • Example:

    MQTTClient.connect('ws://broker.example.com:8083/mqtt', {
      clientId: 'client_id',
      username: 'username',
      password: 'jwt_token',
      protocol: 'ws',
      protocolVersion: 4,
    });

2. subscribe(topic: string)

Subscribes to a specified topic.

  • Parameters:

    • topic: The MQTT topic to subscribe to.
  • Example:

    MQTTClient.subscribe('example/topic');

3. publish(topic: string, message: string)

Publishes a message to a specified topic.

  • Parameters:

    • topic: The MQTT topic to publish to.
    • message: The message payload to be sent.
  • Example:

    MQTTClient.publish('example/topic', 'Hello, MQTT!');

4. onMessage(callback: (topic: string, message: string) => void)

Registers a callback function to handle incoming messages.

  • Parameters:

    • callback: A function that receives the topic and message payload.
  • Example:

    MQTTClient.onMessage((topic, message) => {
      console.log(`Received message on ${topic}: ${message}`);
    });

5. disconnect()

Disconnects from the MQTT broker.

  • Example:
    MQTTClient.disconnect();

License

MIT License

Package Sidebar

Install

npm i rnmrtt-client

Weekly Downloads

5

Version

1.0.13

License

MIT

Unpacked Size

9.15 kB

Total Files

4

Last publish

Collaborators

  • aslinformation