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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mqtt-mosq-chat-client

v1.0.1

Published

A React.js chat client using MQTT

Readme

Mqtt-mosq-chat-client.js is a client library that uses the mqtt.js library with the Mosquitto broker to implement a chat functionality. It communicates with its sister server side library mqtt-mosq-chat-server.js.

Table of Contents

Installation

npm install mqtt-mosq-chat-client

Config file

Creation

The library requires a config file from which it will read some of the data it needs. You will have to create that json file with a name of your choice (for our examples we will use the name config.json). It also needs to be in your project's src directory. The json structure should look something like this example:

{
  "backendUrl": "http://localhost:3001/",
  "brokerUrl": "ws://localhost:8083"
}

Replace the the example data with your own.

Importing the config into your project files

Most of the functions will take the config as an argument. There are two ways that you can import the config in order to use it.

  • Import it in the component that you need it:
import configData from './config.json';
  • Import it in your app router, and then pass it as a parameter in the component that you need it in:
function AppRouter() {

  return (
    <div>
      <Routes>
          <Route exact path="/" element={<App/>} />
          <Route path="/createroom" element={<CreateRoom config={configData}/>} />
          <Route path="/register" element={<Register config={configData}/>} />
          <Route path="/login" element={<Login config={configData}/>} />
          <Route path="/joinroom" element={<JoinRoom config={configData}/>} />
          <Route path="/rooms/*" element={<Rooms config={configData}/>} />
          <Route path="/account" element={<Account config={configData}/>} />
      </Routes>
    </div>
  );
}

export default AppRouter;

Import

import {example} from 'mqtt-mosq-chat-client';

API


registerUser(username, password, config)

Creates an account using the giiven username and password. It sends an axios request to the server library (URL taken from the config) which then saves the info in the mongoDB databse, after it has checked if the credentials are valid. Also stores the unique userID and username in the brower's local storage. Furthermore, it creates a "client" in the Dynamic Security Plugin for the user. Returns a string.

loginUser(username, password, config)

Logs the user in his account by sending an axios request to check the info. Gets the user's userID and saves it in the local storage along with his username.

createRoom(name, startDate, endDate, config)

Creates a chat room with the given name. The startDate and endDate are optional and they dictate the time period for which the room will be active. Also it creates a new role in the Dynamic Security Plugin for the current room (each room has a corresponding role, this way we manage room permissions). Then, it gives the current user permissions to join the room. Returns a string.

joinName(name, config)

Joins the room with the given name, after it makes all the needed checks. It retrieves the user's broker credentials and makes the mqtt connection, then subscribes to that room. The function is async and returns the mqttClient object which is needed as an argument for some other functions.

joinCode(code, config)

Very similar to joinName, but this time we use a random invitation string to join the room. That string is generated by the generatePassword function and is used to invite people to rooms that theydon't have access to.

fetchUserRooms(config)

An async function that makes a request to retrieve all the rooms that the current user has access to, Returns a list with these rooms.

fetchMessageHistory(name, config)

An async function that retrieve the message history of the room with that name. Should be used after a user joins are room. Return an array of objects for each message containing the sender and the message.

useMqttListener(name, client, onMessageReceived)

This is a custom hook for receiving messages in a room. The 3rd argument is the callback function that should handle the messages. For example:

const handleIncomingMessage = (message) => {
  setMessages((prevMessages) => [...prevMessages, message]);
};

useMqttListener(name, mqttClient, handleIncomingMessage);

messageSend(name, messageInput, username, mqttClient)

Packages and publishes the message to the room with that name.

generatePassword(name, config)

A function that generates an invitation code that can be used by a different user to join a room. The invitation is saved in the back end database and expires 1 hour after it is created. Only 1 invitation code can be active at a time. Returns the code.

disconnectMqtt(client, topic)

Unsubscribes from the room and disconnects from the broker.

leaveRoom(name, config)

Removes the user's permissions for that room.

deleteAccount(config)

Deletes the user's account both from the database and the Dynamic Security Plugin.

---s