Skip to content

Commit c4b7ecb

Browse files
authored
Add extra mqtt3 callbacks (#476)
* add extra mqtt3 callbacks `on_connection_success`, `on_connection_failure`, `on_connection_closed`
1 parent c18fd5c commit c4b7ecb

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

awsiot/mqtt_connection_builder.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@
3939
4040
* `**kwargs` (dict): Forward-compatibility kwargs.
4141
42+
**on_connection_success** (`Callable`): Optional callback invoked whenever the connection successfully connects.
43+
The function should take the following arguments and return nothing:
44+
45+
* `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection.
46+
47+
* `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData`): The data returned from the connection success.
48+
49+
**on_connection_failure** (`Callable`): Optional callback invoked whenever the connection fails to connect.
50+
The function should take the following arguments and return nothing:
51+
52+
* `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection.
53+
54+
* `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData`): The data returned from the connection failure.
55+
56+
**on_connection_closed** (`Callable`): Optional callback invoked whenever the connection has been disconnected and shutdown successfully.
57+
The function should take the following arguments and return nothing:
58+
59+
* `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection.
60+
61+
* `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData`): The data returned from the connection close.
62+
4263
**clean_session** (`bool`): Whether or not to start a clean session with each reconnect.
4364
If True, the server will forget all subscriptions with each reconnect.
4465
Set False to request that the server resume an existing session
@@ -230,6 +251,9 @@ def _builder(
230251
use_websockets=use_websockets,
231252
websocket_handshake_transform=websocket_handshake_transform,
232253
proxy_options=proxy_options,
254+
on_connection_success=_get(kwargs, 'on_connection_success'),
255+
on_connection_failure=_get(kwargs, 'on_connection_failure'),
256+
on_connection_closed=_get(kwargs, 'on_connection_closed'),
233257
)
234258

235259

samples/pubsub.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
5858
if received_count == cmdData.input_count:
5959
received_all_event.set()
6060

61+
# Callback when the connection successfully connects
62+
def on_connection_success(connection, callback_data):
63+
assert isinstance(callback_data, mqtt.OnConnectionSuccessData)
64+
print("Connection Successful with return code: {} session present: {}".format(callback_data.return_code, callback_data.session_present))
65+
66+
# Callback when a connection attempt fails
67+
def on_connection_failure(connection, callback_data):
68+
assert isinstance(callback_data, mqtt.OnConnectionFailuredata)
69+
print("Connection failed with error code: {}".format(callback_data.error))
70+
71+
# Callback when a connection has been disconnected or shutdown successfully
72+
def on_connection_closed(connection, callback_data):
73+
print("Connection closed")
6174

6275
if __name__ == '__main__':
6376
# Create the proxy options if the data is present in cmdData
@@ -79,7 +92,10 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
7992
client_id=cmdData.input_clientId,
8093
clean_session=False,
8194
keep_alive_secs=30,
82-
http_proxy_options=proxy_options)
95+
http_proxy_options=proxy_options,
96+
on_connection_success=on_connection_success,
97+
on_connection_failure=on_connection_failure,
98+
on_connection_closed=on_connection_closed)
8399

84100
if not cmdData.input_is_ci:
85101
print(f"Connecting to {cmdData.input_endpoint} with client ID '{cmdData.input_clientId}'...")

0 commit comments

Comments
 (0)