Git Product home page Git Product logo

laravel-mqtt-demo's Introduction

Laravel / MQTT Demo Application

For the entire story behind this setup please see the article on my blog where I describe how we built a Laravel application that used MQTT to log temperature readings via simple sensors.

Node Script

process.title = 'mqtt-demo-process-node'

const mqtt = require('mqtt')
const axios = require('axios')
const debug = process.env.NODE_ENV !== 'production'

let endpoint = 'https://####.test/messages'

if(process.env.NODE_ENV === 'production') {
    endpoint = 'https://####.com/messages'
}

if(debug){
    console.log('connecting')
}

const client = mqtt.connect('mqtt://m16.cloudmqtt.com:#####', {
    username: '####',
    password: '####'
})

client.on('connect', () => {
    if(debug) {
        console.log('connected')
    }

    client.subscribe('+/your-topic',{qos:1})
})

client.on('message',function(topic,message){
    if(debug) {
        console.log('this message :', message.toString());
    }
    axios.post(endpoint, {topic, message: message.toString()})
        .then(({ data }) => {
            if(debug) {
                console.log(data);
            }
        })
        .catch(error => {
            if(debug) {
                console.error(error);
            }
        });
});

Laravel Forge Configuration

We will need to stop the listener from time to time, so set up a daemon to keep yarn mqtt-prod running.

/home/forge/your-site.com yarn mqtt-prod

Deployment Script

Include pkill -f mqtt-demo-process-node in the deployment script so that the mqtt script will be restarted and pick up any new information when the supervisor restarts it.

cd /home/forge/your-site.com
git pull origin master
composer install --no-interaction --prefer-dist --optimize-autoloader
echo "" | sudo -S service php7.3-fpm reload

yarn install

yarn production

# This value is set at the top of the subscriber.js
pkill -f mqtt-demo-process-node 

if [ -f artisan ]
then
    php artisan migrate --force
fi

Artisan Command

This command allows the Laravel app to kill the node mqtt listener. Once the process is stopped the daemon should auto-restart yarn mqtt-prod and pick up any new connections.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Process;

class MQTT extends Command
{
    protected $signature = 'mqtt:kill-process';

    protected $description = 'Stop the overseer mqtt node process';

    public function handle()
    {
        $process = new Process(['pkill', '-f', 'mqtt-demo-process-node']);
        $process->start();

        foreach ($process as $type => $data) {
            if ($process::OUT === $type) {
                echo "\nRead from stdout: ".$data;
            } else {
                echo "\nRead from stderr: ".$data;
            }
        }
    }
}

laravel-mqtt-demo's People

Contributors

dependabot[bot] avatar jesseschutt avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.