0

I have an yii2 project with the advanced template and I want to implement notifications using a mosquitto broker. I already have the publish part done and working, now I'd like to have some help on subscribing to a topic on my frontend app. I already tried, but the page seems to stop working when I subscribe to any topic. Is there any easy way or tutorial that I can use? If any more information is needed, please ask.

P.S: My idea was: When I open any page on frontend, I check for messages, save them in a array, set them as view param and then render my page.

EDIT: So far i've tried the following

Class

<?php

namespace common\models;

use Yii;


class Notificacoes
{
    private $listaNotificacoes;

    public function __construct($id, $name)
    {
        $this->listaNotificacoes = array();

        $server = "127.0.0.1";     
        $port = 1883;                     
        $username = "";                   
        $password = "";                   
        $client_id = $id;

        $mqtt = new \common\mosquitto\phpMQTT($server, $port, $client_id);

        if(!$mqtt->connect(true, NULL, $username, $password)) {
            exit(1);
        }

        $topics[$name] = array("qos" => 0, "function" => "procmsg");
        $mqtt->subscribe($topics, 0);

        while($mqtt->proc()){

        }
        $mqtt->close();
    }
    function procmsg($topic, $msg)
    {
        \array_push($this->listaNotificacoes, $msg);
    }

    public function getAll()
    {
        return $this->listaNotificacoes;
    }
}

SiteController: I tried to get the messages on the beforeAction method

public function beforeAction($action)
    {
        if (!parent::beforeAction($action)) {
            return false;
        }

        $notifications = array();

        if (!Yii::$app->user->isGuest) 
        {
            $notifs = new Notificacoes(Yii::$app->user->identity->getId(), Yii::$app->user->identity->username);
            $notifications = $notifs->getAll();
        }

        $this->view->params['notifications'] = $notifications;

        return true; 
    }
  • Asking for tutorials is off topic for Stack Overflow. Edit the question to SHOW what you've already tried and somebody may be able to help you fix it. – hardillb Jan 21 '18 at 16:59
  • Edited! Thanks for the tip. I asked for a tutorial because in this case I don't believe there's much I can do with the code I have, but I might be wrong – Leonardo Domingues Jan 21 '18 at 18:55

1 Answers1

0

This model is unlikely to work because normally messages are only delivered by MQTT at the instant they are published.

So unless you are using retained messages or persistent subscriptions for a given client id to queue messages. This means your while loop will never have any messages to process

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • That's what I was afraid. I was told to make a notification system using mqtt, but I will try to change for a BD record based one. Thanks for the response – Leonardo Domingues Jan 21 '18 at 20:25
  • You could just have the page subscribe to the correct MQTT topic using MQTT over Websockets. This would have the benefit of being realtime and not needing wait until the a page loads to get messages – hardillb Jan 21 '18 at 20:27
  • I'm not sure i'm able to apply that with my current conditions (school project using wamp and my laptop). Besides, it's easier for me code this system using a table in my bd (since i'm used to it) than investing time right now to learn it. Maybe in another project. Thanks again – Leonardo Domingues Jan 21 '18 at 20:32