Skip to content

Commit dc40a6d

Browse files
authored
Fix: use blocking socket only during publishing if configured (#141)
1 parent b1759ef commit dc40a6d

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,11 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
142142
// The password used for authentication when connecting to the broker.
143143
->setPassword(null)
144144

145-
// Whether to use a blocking socket or not. By default, the socket is non-blocking,
146-
// which is required when using subscriptions and/or {@see MqttClient::loop()}.
147-
// In rare cases, it might be required to use a blocking socket though. One such example
148-
// is when sending large messages (e.g. binaries) and the broker has a limited receive buffer.
149-
//
150-
// Note: When using a blocking socket, the MQTT client can get stuck if the socket is broken
151-
// or when the broker does not consume the sent data fast enough. Use with caution.
145+
// Whether to use a blocking socket when publishing messages or not.
146+
// Normally, this setting can be ignored. When publishing large messages with multiple kilobytes in size,
147+
// a blocking socket may be required if the receipt buffer of the broker is not large enough.
148+
//
149+
// Note: This setting has no effect on subscriptions, only on the publishing of messages.
152150
->useBlockingSocket(false)
153151

154152
// The connect timeout defines the maximum amount of seconds the client will try to establish

src/ConnectionSettings.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,11 @@ public function getPassword(): ?string
8282
}
8383

8484
/**
85-
* Whether to use a blocking socket or not. By default, the socket is non-blocking,
86-
* which is required when using subscriptions and/or {@see MqttClient::loop()}.
87-
* In rare cases, it might be required to use a blocking socket though. One such example
88-
* is when sending large messages (e.g. binaries) and the broker has a limited receive buffer.
85+
* Whether to use a blocking socket when publishing messages or not.
86+
* Normally, this setting can be ignored. When publishing large messages with multiple kilobytes in size,
87+
* a blocking socket may be required if the receipt buffer of the broker is not large enough.
8988
*
90-
* Note: When using a blocking socket, the MQTT client can get stuck if the socket is broken
91-
* or when the broker does not consume the sent data fast enough. Use with caution.
89+
* Note: This setting has no effect on subscriptions, only on the publishing of messages.
9290
*
9391
* @param bool $useBlockingSocket
9492
* @return ConnectionSettings
@@ -97,7 +95,7 @@ public function useBlockingSocket(bool $useBlockingSocket): ConnectionSettings
9795
{
9896
$copy = clone $this;
9997

100-
$copy->useBlockingModeForSocket = $useBlockingSocket;
98+
$copy->useBlockingSocket = $useBlockingSocket;
10199

102100
return $copy;
103101
}

src/MqttClient.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ protected function establishSocketConnection(): void
290290
}
291291

292292
stream_set_timeout($socket, $this->settings->getSocketTimeout());
293-
stream_set_blocking($socket, $this->settings->shouldUseBlockingSocket());
293+
stream_set_blocking($socket, false);
294294

295295
$this->logger->debug('Socket opened and ready to use.');
296296

@@ -1140,8 +1140,16 @@ protected function writeToSocket(string $data, int $length = null): void
11401140
$calculatedLength = strlen($data);
11411141
$length = min($length ?? $calculatedLength, $calculatedLength);
11421142

1143+
if ($this->settings->shouldUseBlockingSocket()) {
1144+
socket_set_blocking($this->socket, true);
1145+
}
1146+
11431147
$result = @fwrite($this->socket, $data, $length);
11441148

1149+
if ($this->settings->shouldUseBlockingSocket()) {
1150+
socket_set_blocking($this->socket, false);
1151+
}
1152+
11451153
if ($result === false || $result !== $length) {
11461154
$this->logger->error('Sending data over the socket to the broker failed.');
11471155
throw new DataTransferException(

0 commit comments

Comments
 (0)