How to run mqtt connection when application is closed

hello friend I’m making an application that connects to the mqtt protocol I found a little problem when the application I was running was closed the connection to the mqtt protocol was lost, the question is how to solve this, is it possible to give me an example