/Mosca_MQTTjs_MySQL_MongoDB

JavaScript-based MQTT: Mosca, MQTT.js, MySQL & MongoDB

Primary LanguageJavaScript

simplinnovation

Mosca 💛 MQTTjs 💚 MySQL + MongoDB

1. Mosca & MQTT.js

  • Click & watch video tutorial below! 👇

    Mosca MQTT.js

  • Initiate a Node.js project then install Mosca & MQTT.js:

    $ npm init
    $ npm i mosca mqtt
  • Create an MQTT broker (broker.js):

    // Mosca MQTT broker
    var mosca = require('mosca')
    var settings = {port: 1234}
    var broker = new mosca.Server(settings)
    
    broker.on('ready', ()=>{
        console.log('Broker is ready!')
    })
    
    broker.on('published', (packet)=>{
        message = packet.payload.toString()
        console.log(message)
    })
  • Create an MQTT subscriber (sub.js):

    // MQTT subscriber
    var mqtt = require('mqtt')
    var client = mqtt.connect('mqtt://localhost:1234')
    var topic = 'LINTANGtest123'
    
    client.on('message', (topic, message)=>{
        message = message.toString()
        console.log(message)
    })
    
    client.on('connect', ()=>{
        client.subscribe(topic)
    })
  • Create an MQTT publisher (pub.js):

    // MQTT publisher
    var mqtt = require('mqtt')
    var client = mqtt.connect('mqtt://localhost:1234')
    var topic = 'LINTANGtest123'
    var message = 'Hello World!'
    
    client.on('connect', ()=>{
        setInterval(()=>{
            client.publish(topic, message)
            console.log('Message sent!', message)
        }, 5000)
    })

2. Mosca, MQTT.js & MySQL

  • Click & watch video tutorial below! 👇

    Video

  • Create a database & table on MySQL:

    $ create database mqttJS;
    $ use mqttJS
    $ create table(
        id int not null auto_increment,
        message varchar(255),
        time timestamp default current_timestamp,
        primary key (id)
    );
    $ describe mqttJS
  • Install MySQL.js:

    $ npm i mysql
  • Create an MQTT broker (brokerMySQL.js):

    // MQTT broker
    var mosca = require('mosca')
    var settings = {port: 1234}
    var broker = new mosca.Server(settings)
    
    // MySQL 
    var mysql = require('mysql')
    var db = mysql.createConnection({
        host: 'localhost',
        user: 'lintang',
        password: '12345',
        database: 'mqttJS'
    })
    db.connect(()=>{
        console.log('Database connected!')
    })
    
    broker.on('ready', ()=>{
        console.log('Broker is ready!')
    })
    
    broker.on('published', (packet)=>{
        message = packet.payload.toString()
        console.log(message)
        if(message.slice(0,1) != '{' && message.slice(0,4) != 'mqtt'){
            var dbStat = 'insert into mqttJS set ?'
            var data = {
                message: message
            }
            db.query(dbStat, data, (error, output)=>{
                if(error){
                    console.log(error)
                } else {
                    console.log('Data saved to database!')
                }
            })
        }
    })

3. Mosca, MQTT.js & MongoDB

  • Click & watch video tutorial below! 👇

    Video

  • Create a database & collection on MongoDB:

    $ use mqttJS
    $ db.createUser({
        'user': 'lintang',
        'pwd': '12345',
        'roles': ['readWrite', 'dbAdmin']
    })
    $ db.createCollection('mqttJS')
  • Install MongoDB.js:

    $ npm i mongodb
  • Create an MQTT broker (brokerMongoDB.js):

    // MQTT broker
    var mosca = require('mosca')
    var settings = {port: 1234}
    var broker = new mosca.Server(settings)
    
    // MongoDB
    var mongo = require('mongodb')
    var mongc = mongo.MongoClient
    var url = 'mongodb://lintang:12345@localhost:27017/mqttJS'
    
    broker.on('ready', ()=>{
        console.log('Broker is ready!')
    })
    
    broker.on('published', (packet)=>{
        message = packet.payload.toString()
        console.log(message)
        if(message.slice(0,1) != '{' && message.slice(0,4) != 'mqtt'){
            mongc.connect(url, (error, client)=>{
                var myCol = client.db('mqttJS').collection('mqttJS')
                myCol.insertOne({
                    message: message
                }, ()=>{
                    console.log('Data is saved to MongoDB')
                    client.close()
                })
            })
        }
    })

Lintang Wisesa 💌 lintangwisesa@ymail.com

Facebook | Twitter | LinkedIn | Youtube | :octocat: GitHub | Hackster