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

mqtt-mosq-chat-server

v1.0.0

Published

A Node.js chat server using MQTT, Mosquitto Broker and MongoDB

Downloads

1

Readme

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

Table of Contents

Setting up the Mosquitto broker & Plugin

On Ubuntu Linux, execute the following commands to install the Mosquitto broker:

sudo apt update
sudo apt install mosquitto

The Mosquitto broker should start automatically after installation. If it's not running, you can start it manually using the following command:

sudo systemctl start mosquitto

You can check the status of the Mosquitto broker using the following command:

sudo systemctl status mosquitto

If you want the Mosquitto broker to start automatically when your server boots up, you can enable it using the following command:

sudo systemctl enable mosquitto

To configure the Mosquitto broker, you can make changes to its configuration file, typically named mosquitto.conf. The location of this file may vary depending on your operating system and installation method, but usually it is located in /etc/mosquitto. Open with an editor of your choice and add the following lines, if they don't already exist:

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

listener 8083
protocol websockets

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

At this point we need to install the Dynamic Security Plugin for Mosquitto. On Linux you would expect the plugin library to be installed to /usr/lib/x86_64-linux-gnu/mosquitto_dynamic_security.so or a similar path, but this will vary depending on the particular distribution and hardware in use.

We need to add some more lines to our mosquitto.conf file. Those lines are:

plugin path/to/mosquitto_dynamic_security.so
plugin_opt_config_file path/to/dynamic-security.json

allow_anonymous false
per_listener_settings false

Replace "path/to/" with the actual path to the .so and .json files. Note that we still haven't created the dynamic-security.json, we will do it very soon.

The "allow_anonymous false" option will allow only registered users to connect to the broker, while the "per_listener_settings false" option is so all listeners use the same authentication and access control.

The next step is to create the dynamic-security.json file, which is where the plugin configuration will be stored. This file will be updated each time you make client/group/role changes, during normal operation the configuration stays in memory. To generate an initial file, we will use the mosquitto_ctrl utility to create our first and also admin user. We do that by executing the following command (again replace path/to/ with the path you want, I recommend the same path as your mosquitto.conf):

mosquitto_ctrl dynsec init path/to/dynamic-security.json admin-user

Make sure the path to the .json file is correct in the .conf file as well.

You can choose your own admin-user username. You will be asked for a password for the client. This user will be assigned the admin role.

Now that the dynamic-security.json is created, we need to change the read/write permissions of the file. Go to the directory where you created the dynamic-security.json file and execute this command:

sudo chmod a+rw dynamic-security.json

Everything should be set up now.

Installation

npm install mqtt-mosq-chat-server

Config file

The library requires a config file from which it will read some of the data it needs. You will have to create that file with the name config.json. It also needs to be in the same directory as your project's entry file (usually the index.js file). The json structure should look something like this example:

{
  "databaseUrl": "mongodb://localhost:27017/libtestDB",
  "brokerUrl": "ws://localhost:8083",
  "port": "3001",
  "credentials": {
    "username": "admin-user",
    "password": "admin"
  }
}

Replace the the example data with your own.

Import

const chatServer = require('mqtt-mosq-chat-server')

API


chatServer.connectDB()

Uses the URL in the config file to connect to the mongoDB database.

chatServer.brokerConnect()

Creates a listener that automatically subscribes to all the chat rooms that are saved in the database as soon as the connection to the broker is achieved.

chatServer.receivingMessages()

Creates a listener for all the messages that the server will receive. The server will be subscribed to all the existing rooms so it will receive every single messages sent. For each message, it will save it in the mongoDB database.

chatServer.brokerError()

Creates a listener for any errors that might occur with the broker connection.

chatServer.startServer()

The main function that starts the express server listener in the given port. It sets the endpoints for the various axios requests that the client library will send.


Example

An example of how you can initialize the library from your idnex.js file:

const chatServer = require('mqtt-mosq-chat-server');

chatServer.connectDB();

chatServer.brokerConnect();

chatServer.receivingMessages();

chatServer.brokerError();

chatServer.startServer();