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

rnmrtt-client

v1.0.13

Published

A React Native MQTT client with JWT support

Downloads

997

Readme

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