McFizh/libMQTT

Subscribe problem

Closed this issue · 10 comments

Hello i have a problem with subscribe

Warning: fwrite() expects parameter 1 to be resource, null given in /opt/lampp/htdocs/Client.php on line 415

Warning: fwrite() expects parameter 1 to be resource, null given in /opt/lampp/htdocs/Client.php on line 416

Warning: feof() expects parameter 1 to be resource, null given in /opt/lampp/htdocs/Client.php on line 687
i recibe this error

this is the code

$client = new LibMQTT\Client($host,$port,"ClientID".rand());
$client->setAuthDetails($username,$password);
$client->setCryptoProtocol("tls");
$client->setVerbose(1);
$result = $client->subscribe([$topic => [ "qos" => 1 ]]);

You are missing connect method before subscribe. I've created new version of the library (1.0.1) which should now give out more sensible error message is this kind of case.

Yes I know

but when I connect, it makes this mistake

libmqtt: Connecting to: tcp://m21.cloudmqtt.com:35631 libmqtt: Connection failed! Server gave unexpected response.

I think the problem is that it connects by tcp when it is a socket should be ws or is there any way to specify this parameter?

Afaik PHP doesn't have native websocket support. You should use the SSL-port on cloudmqtt.

https://www.cloudmqtt.com/images/cloudmqttdetails.png

I thought that in order to subscribe you needed to connect to the websocket, but if the other ports can be used, I'm fine, but it gives me this error.
but doing publish () works

libmqtt: Connecting to: tls://m21.cloudmqtt.com:25631 libmqtt: Connection failed! Server gave unexpected response. libmqtt: Invalid SUBACK packet received (stage 1) libmqtt: DISCONNECT sent

I tested the library against "cute cat" plan on cloudmqtt and had no problems. Could you tell me the PHP version you are using and the topic you are subscribing to, so I can try it myself?

Yep
I was using a private account, I just created another to test you passed the data.
PHP version : 5.6.31
url: m20.cloudmqtt.com
$topic = "1234";
Its a private device.

I tried this code on PHP 5.6.31 and didn't get any errors:

-- clip clip clip --

$client = new LibMQTT\Client("m22.cloudmqtt.com",20381,"ClientID".rand());
$client->setCryptoProtocol("tls");
$client->setVerbose(1);
$client->setAuthDetails("xxxxx", "xxxxxxx");

$client->connect();
$client->publish("test", "Test message 1", 0);
$client->subscribe( [ "1234" => [ "qos" => 1 ] ] );
$client->publish("1234", "Test message 2", 0);
$client->close();

-- clip clip clip --

I've tried it as you have it and the same error
I do not understand the reason, I have tried it in the XAMPP in local and in my remote server and the same T_T

libmqtt: Connecting to: tls://m20.cloudmqtt.com:26377
libmqtt: Connection failed! Server gave unexpected response.
libmqtt: Invalid SUBACK packet received (stage 1)
libmqtt: DISCONNECT sent
sinapptic@hl109:~/www$ php send.php
libmqtt: Connecting to: tls://m20.cloudmqtt.com:26377
libmqtt: Connection failed! Server gave unexpected response.
libmqtt: Invalid SUBACK packet received (stage 1)
libmqtt: DISCONNECT sent

Can you connect to the server (and subscribe to topic) without using tls ?

I get the same error