sskaje / mqtt

MQTT Client class
https://sskaje.me/category/MQTT/
MIT License
86 stars 33 forks source link

Unable to Subscribe when Published #7

Closed srikodali-eruvaka closed 8 years ago

srikodali-eruvaka commented 8 years ago

I am able to publish using publish.php. But when I am trying to run subscribe.php, the page was loading..... but I can't see any result. Can you please tell me the mistake I am doing.

sskaje commented 8 years ago

page loading? You are running this script from browser rather than Conmand Line Interface?

2015年9月21日星期一,srikodali-eruvaka notifications@github.com 写道:

I am able to publish using publish.php. But when I am trying to run subscribe.php, the page was loading..... but I can't see any result. Can you please tell me the mistake I am doing.

— Reply to this email directly or view it on GitHub https://github.com/sskaje/mqtt/issues/7.

sskaje@gmail.com https://sskaje.me/

srikodali-eruvaka commented 8 years ago

Yes, I am running php page. I want to check whether subscribe is working or not. In my project, I am using MQTT to communicate between sensor device and cloud DB. So when ever device publish any data I need to subscribe it and based on data received I need to reply with publish.

So for this, before trying communication between device and cloud, I am trying to publish & subscribe from the examples folder of yours. From your words I think I can't run subscribe.php, if so can you help to achieve my project goal.

sskaje commented 8 years ago

CLI is a best choice if you want to do similar things.

Run php in screen like: screen php /path/to/subscribe.php

In the subscribe.php, call functions in other files or call your php web page for actions. DO NOT run the infinite loop in your php web page.

sskaje@gmail.com https://sskaje.me/

On Tue, Sep 22, 2015 at 12:17 PM, srikodali-eruvaka < notifications@github.com> wrote:

Yes, I am running php page. I want to check whether subscribe is working or not. In my project, I am using MQTT to communicate between sensor device and cloud DB. So when ever device publish any data I need to subscribe it and based on data received I need to reply with publish.

So for this, before trying communication between device and cloud, I am trying to publish & subscribe from the examples folder of yours. From your words I think I can't run subscribe.php, if so can you help to achieve my project goal.

— Reply to this email directly or view it on GitHub https://github.com/sskaje/mqtt/issues/7#issuecomment-142178125.

srikodali-eruvaka commented 8 years ago

For example if I am using below code for my Device Cloud communication using http <?php if(isset($_POST['id'])){ $rawId = $_POST['id']; $rawVal = $_POST['id']; $sql = mysql_query("INSERT INTO table VALUES ('',$rawId,$rawVal)"); $qry = mysql_fetch_assoc(mysql_query("SELECT $rawVal FROM table WHERE $rawId='5'")); echo $qry['$rawVal']>0?$qry['$rawVal']:0; } ?>

Can you please tell me how can I achieve this using subscribe.php.

I am doing some research on screen, as it is throwing following error: No results found in /var/run/screen/S-user

srikodali-eruvaka commented 8 years ago

I tested with screen php /path to/subscribe.php

It connected but when I published it is throwing error like:

A PHP Error was encountered

Severity: Warning

Message: call_user_func() expects parameter 1 to be a valid callback, function 'default_subscribe_callback' not found or invalid function name

Filename: third_party/spMQTT.class.php

Line Number: 521

Code which I am using: publish.php: <?php class TestPub extends CI_Controller{ public function index(){ $this->load->library('mos_mqtt'); $mqtt = new spMQTT('tcp://test.mosquitto.org:1883/'); spMQTTDebug::Enable(); //$mqtt->setAuth('sskaje', '123123'); $connected = $mqtt->connect(); if (!$connected) { die("Not connected\n"); } echo "Connected"; $mqtt->ping(); $msg = "Testing MQTT with new library"; $mqtt->publish('cherryTest', $msg, 0, 1, 0, 1);
} } ?>

subscribe.php: <?php class TestSub extends CI_Controller{ public function index(){ $this->load->library('mos_mqtt'); $mqtt = new spMQTT('tcp://test.mosquitto.org:1883/'); spMQTTDebug::Enable(); //$mqtt->setAuth('sskaje', '123123'); $mqtt->setKeepalive(3600); $connected = $mqtt->connect(); if (!$connected) { die("Not connected\n"); } //$topics['sskaje/#'] = 1; $topics['cherryTest'] = 1; $mqtt->subscribe($topics);

$mqtt->unsubscribe(array_keys($topics));

    $mqtt->loop('default_subscribe_callback');
    /**
     \* @param spMQTT $mqtt
     \* @param string $topic
     \* @param string $message
     */
    function default_subscribe_callback($mqtt, $topic, $message) {
        printf("Message received: Topic=%s, Message=%s\n", $topic, $message);
    }
}

} ?>

sskaje commented 8 years ago

try latest version