Contributing to mqtt-recorder project
Tags: python, mqtt, opensource
Published: 2023-07-19 16:53:50

Recently I just contributed to an open source project. The link for the project is as following: 
- [MQTT Recorder](https://github.com/rpdswtk/mqtt_recorder)

This is a python library that I use to record messages that are published by MQTT. The reason why I want to record the messages is because I want to play it back another time, so I don’t have to, let say, make the sensors/hardware push the data again.

This library is already working great but I see the opportunity for me to contribute to an open source project, by adding a few new features to this library.


## What it is

MQTT Recorder - That’s the name of this library. It is a simple command line tool for recording and replaying MQTT messages. 

### Record
To record MQTT messages, you can simply run as following:
`mqtt-recorder --host localhost --mode record --file recording.csv`

What it does is it will connect to the MQTT broker that is hosted in **localhost**, record the MQTT messages, and store it into **recording.csv**.

Once you finish recording the messages, you can stop it from recording by **CTRL+C**.

### Replay
To replay the recording, simply run as following:
`mqtt-recorder --host localhost --mode replay --file recording.csv`

## Contribution

Previously, I only showed the basic use of recording and replaying the messages. But when using it, I need to add my own configuration, such as using username and password for authentication, defining client_id for identity, and using SSL for secure data transmission. However, in **version 1.2.0**, it does not support **client_id** and to use **insecure TLS certs**. 
Hence, I added two new features which are, passing a value to parameter client_id and enable insecure TLS.

First, I modified the _**\_\_main\_\_.py**_ file in **mqtt_recorder**. I added 2 arguments, where to define **_--client_id_** and **_--tls_insecure_**.


![--client_id argument](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnnsdjaf10dw5h4l9151.png)


![--tls-insecure argument](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6pxxpk953vyi3vqiyrg.png)


So **_--client_id_** will receive the input as a **string**, while **_--tls_insecure_** will receive the input as a **boolean**.

Then go to the **_main()_** function, to pass the arguments in the function.

I defined the argument **_client_id_** inside the **MqttRecorder** class. The code is:
`args.client_id`


At the same time, I defined the argument for **_--tls_insecure_**, which is located in the **SslContext** class. The code is:
`args.tls_insecure`


![__main__.py](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjdzodhdlvefp96qpa2l.png)

Now, I have added the code for defining parameters at _**\_\_main\_\_.py**_.

Proceed on, the file that is needed to modify is **_recorder.py_**, located at **mqtt_recorder**.

Go to **_SslContext_** class, to add the argument **_tls_insecure_**, and define the variable. An additional line of code to receive the value based on the parameters was added.


![SslContext()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76u7w0hv4wysbwpi5i63.png)

Next, go to **MqttRecorder** class. In _**\_\_init\_\_**_ function, to define parameter **_client_id_** with a string type.
`client_id: str`


After the parameter is defined, it can now be used in the MQTT library. The **_client_id_** can now be passed to the MQTT Client class.
`self.__client = mqtt.Client(client_id=client_id)`


Lastly, an if statement for tls_insecure was added. This is only needed if the **_--enable_ssl_** parameter is True. The if statement is used only when the parameter is passed, then the **_tls_insecure_set_** function is executed.


![MqttRecorder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zee5e3qzu58m7tmjudag.png)


That's how you add client_id and enable insecure TLS features. I hope this helps!



**Notes:**
|> 20 July 2023 - My pull request still not merge: https://github.com/rpdswtk/mqtt_recorder/pull/5, however you can use the updated version here: https://github.com/Tester2009/mqtt_recorder

|> 7 August 2023 - Finally, my pull request has been merged on 5th August, 2023.