bluerhinos / phpMQTT

a simple php class to connect/publish/subscribe to a MQTT broker
Other
770 stars 461 forks source link

Not working on cpanel after upload. #62

Closed tareq0065 closed 4 years ago

tareq0065 commented 6 years ago

I have tried many servers to use, but it's not working. I am always getting the time out. Is there any special requirement to use the server? Or if I missing something on settings? Actually, I have some dynamic form that contains dynamic fields. After posting any of form I want to do a publish action to the mqtt server. It's working on localhost (wamp server) very good. But after uploading it on my Cpanel hosting it's showing me "time out"

        $server = $config['mqtt_settings']['mqtt_server'];
        $port = $config['mqtt_settings']['mqtt_port'];
        $username = $config['mqtt_settings']['mqtt_username'];
        $password = $config['mqtt_settings']['mqtt_password'];
        $client_id = md5(uniqid(rand(), true));
        $topic = $params['topic_name'];
        unset($params['topic_name']);
        $message = json_encode($params);

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

        if ($mqtt->connect(true, NULL, $username, $password)) {
            $mqtt->publish($topic, $message, 0);

            $ret = [
                'status' => 'success',
                'msg' => "you have successfully published to " . $topic
            ];
                'op_by' => $config['user']->uid
            ]);
            $mqtt->close();
        } else {
            $ret = [
                'status' => 'warning',
                'msg' => "Time out"
            ];
        }
leonrenkema commented 6 years ago

Is there a MQTT server running? Looks like the code cannot connect to the server.

tareq0065 commented 6 years ago

How can I check this that my server is good for MQTT or not? Do you have any suggestion?

tareq0065 commented 6 years ago

Also yes at first I guess this too as you said. But I have checked with multiple MQTT servers from cloudMQTT. They are working well at localhost.

leonrenkema commented 6 years ago

You should make sure that $server/$port is filled with the correct server. If that is the case there is maybe some sort of firewall blocking the connection. You should ask the company that is hosting the server.

suraj050kumar commented 6 years ago

Hi Guys,

Have you got any solution for this, i have the same error also.

Thank you

bluerhinos commented 4 years ago

I am restarting this project so having a clear out of old issues (If you believe this is still valid please reopen)

Canned Response: Subscribe is not designed for web requests, it should be used in a long running process. I do plan to add a subscribeAndWaitForMessage() function #95