mqtt icon indicating copy to clipboard operation
mqtt copied to clipboard

WTFFF! (Exception PHP Notice: fwrite(): send of x bytes failed with errno=11 Resource temporarily unavailable in ..ocketClient.php on line 166) [FIX]

Open nielsenaa opened this issue 7 years ago • 2 comments

On some environments (encoutered on centos 6 / 7 + php 5.3x /5.4x) , when publishing big content, with any Qos, (for me, it choked on a little bit more over 42kb, Qos2) , you can get a PHP Notice: fwrite(): send of x bytes failed with errno=11 Resource temporarily unavailable in /tmp/mqtt/mqtt/SocketClient.php on line 166 ..

Indeed, it seems that write($packet, $packet_size) in SocketClient.php sometimes returns Null or 0 instead of false or length written.

I know mqtt is not meant for big content, but some printers use the mqtt protocol, and sometimes you need to pass really big files for pdfs or many images. (and i also already had invested too much time into this not solving it)

I modified the write($packet, $packet_size) function in SocketClient.php on line 175 like so :


    /**
     * Send data
     *
     * @param string $packet
     * @param int    $packet_size
     * @return int
     */
    public function write($packet, $packet_size)
    {
        if (!$this->socket || !is_resource($this->socket)) return false;
        Debug::Log(Debug::DEBUG, "socket_write(length={$packet_size})", $packet);
        
      
       
       #add
        do { $packet = substr($packet, fwrite($this->socket, $packet)); } while (!empty($packet));
       
        If (!empty($packet)) { 
        return false; 
        } else {
        return $packet_size;
        }
       #endadd

      #disabled
      #return fwrite($this->socket, $packet, $packet_size);
    }


It is ugly, but at least now it works flawless (at least for me) . It forces fwrite to do writing correctly, and if not return expected value.

Thanks a thousands to Sskaje who seriously took a lot of personal time debugging my stuff actively and hanging with me for quite a long time and thanks again for his usefull mqtt php library.

nielsenaa avatar Jul 16 '17 04:07 nielsenaa

@nielsenaa did you try

mosquitto_pub -h xxx -q 2 -f test.payload -t xxxx 

from your php server?

btw, I'm considering let fwrite() size configurable.

sskaje avatar Jul 16 '17 06:07 sskaje

oh we crossposted, sorry ! no i did not, will try and see what it says; what should i pay attention for? Want me to delete the pull request, maybe i was too quick;? I tried stream_set_write_buffer but failed ; is your idea somehow like in the comment that pitched me my patchy solution ? https://stackoverflow.com/questions/13390753/php-stream-write-buffer Many thanks again Sskaje

nielsenaa avatar Jul 16 '17 07:07 nielsenaa