Project to IDG2001 class at NTNU. It should simulate MQTT messaging between publishers (smart sensors), broker and subscribers.
Get started in several quick steps
- Install dependencies
npm install
- Enter your DB_URL in
.env
DB_URL = '<ENTER YOUR DB_URL>'
// mongodb+srv://<user>:<password>@<cluster_hostname>/<DB_name>?retryWrites=true&w=majority
- Start the broker
npm start
- Add a sensor (publisher)
- Open a new terminal window
node ./Class/publisher.js
- Add a client (subscriber)
- Open a new terminal window
node ./Class/subscriber.js
- Event 'client':
- When a client connects to the broker ('connect' event initiated by the client)
- Event 'subscribe':
- When a client subscribes to one or more topics ('subscribe' event initiated by the client)
- Stores the client in mongoDB to collection
Subscribers
with topics it had subscribed to (if it does not exist yet)
- Event 'publish':
- When a client publishes a message to the broker ('publish' event initiated by the client)
- Converts message to JSON - differs through contentType
- Stores the publisher in mongoDB to collection
Publishers
(if it does not exist yet) - Stores the message in mongoDB to collection
Messages
- Event 'unsubscribe':
- When a client unsubscribes from one or more topics
- Event 'clientDisconnect':
- When a client disconnects from the broker (closing the terminal window with pub/sub)
- Deletes publisher/subscriber from
Publishers
/Subscribers
collection respectively
Publishers have a choice of connecting to eiter one of the rooms
below. Then the code chooses randomly which format of the message it will be sending from messageTypes
:
const rooms = ['Bedroom', 'Garage', 'LivingRoom', 'Basement'];
const messageTypes = ['senml+json', 'senml+xml', 'senml+exi'];
You can start one or more publishers. When starting a publisher.js
:
- Enter name of the publisher (sensor),
- Enter a topic to which you want the sensor to connect,
- Enter the contentType of the messages sensor should send,
- Enter a unit reported by the sensor,
- The publisher starts sending messages every 7.5 seconds to the broker in format:
{
contentType: `application/${this.payload}`,
data: {
bn: client.options.clientId,
u: this.unit,
n: this.name,
v: getRandomIntInRange(-5, 35),
t: Date.now()
}
}
Subsribers have a choice of connecting to any of the topics below. You can choose more than one topic. Then the code starts to listen to any of the messages published by a publisher.
const rooms = ['Bedroom', 'Garage', 'LivingRoom', 'Basement'];
You can start one or more subscribers. When starting a subscriber.js
:
- Enter subscriber name,
- Enter one or more topics,
- The subscriber starts to listen to the broker and receives messages destined to the topics it subscribed to.