Skip to content

Commit fe0cb88

Browse files
iVampireSPNamoshek
andauthored
Add auto reconnect support (#31)
* Update mqtt-client.php * Update ConnectionManager.php * Update mqtt-client.php * Update mqtt-client.php * Update mqtt-client.php * Update ConnectionManager.php * Update mqtt-client.php * Add and normalize auto-reconnect settings * Add .gitkeep * Remove .gitkeep Co-authored-by: Marvin Mall <marvin-mall@msn.com>
1 parent 136ce2c commit fe0cb88

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

config/mqtt-client.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@
101101
// The interval (in seconds) in which the client will send a ping to the broker,
102102
// if no other message has been sent.
103103
'keep_alive_interval' => env('MQTT_KEEP_ALIVE_INTERVAL', 10),
104+
105+
// Additional settings for the optional auto-reconnect. The delay between reconnect attempts is in seconds.
106+
'auto_reconnect' => [
107+
'enabled' => env('MQTT_AUTO_RECONNECT_ENABLED', false),
108+
'max_reconnect_attempts' => env('MQTT_AUTO_RECONNECT_MAX_RECONNECT_ATTEMPTS', 3),
109+
'delay_between_reconnect_attempts' => env('MQTT_AUTO_RECONNECT_DELAY_BETWEEN_RECONNECT_ATTEMPTS', 0),
110+
],
111+
104112
],
105113

106114
],

src/ConnectionManager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ protected function buildConnectionSettings(array $config): ConnectionSettings
177177
->setLastWillTopic(Arr::get($config, 'last_will.topic'))
178178
->setLastWillMessage(Arr::get($config, 'last_will.message'))
179179
->setLastWillQualityOfService((int) Arr::get($config, 'last_will.quality_of_service', MqttClient::QOS_AT_MOST_ONCE))
180-
->setRetainLastWill((bool) Arr::get($config, 'last_will.retain', false));
180+
->setRetainLastWill((bool) Arr::get($config, 'last_will.retain', false))
181+
->setReconnectAutomatically((bool) Arr::get($config, 'auto_reconnect.enabled', false))
182+
->setMaxReconnectAttempts((int) Arr::get($config, 'auto_reconnect.max_reconnect_attempts', 3))
183+
->setDelayBetweenReconnectAttempts((int) Arr::get($config, 'auto_reconnect.delay_between_reconnect_attempts', 0));
181184
}
182185
}

0 commit comments

Comments
 (0)