# MQTT client connection alive even when app is closed

**URL:** <https://community.hivemq.com/t/mqtt-client-connection-alive-even-when-app-is-closed/366>\
**Category:** Uncategorized\
**Created:** [November 9, 2020, 9:42am UTC](https://community.hivemq.com/t/mqtt-client-connection-alive-even-when-app-is-closed/366 "2020-11-09T09:42:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalpana](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kalpana](https://community.hivemq.com/u/kalpana)\
**Post date:** [November 9, 2020, 9:42am UTC](https://community.hivemq.com/t/mqtt-client-connection-alive-even-when-app-is-closed/366/1 "2020-11-09T09:42:14Z")

</div>

How to keep MQTT client connection alive even when app is closed.how to achieve it

---

<div class="post-metadata">

**Author:** ![DKrueger](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@DKrueger](https://community.hivemq.com/u/DKrueger)\
**Post date:** [November 9, 2020, 2:37pm UTC](https://community.hivemq.com/t/mqtt-client-connection-alive-even-when-app-is-closed/366/2 "2020-11-09T14:37:38Z")

</div>

Hey,

I´m not able to give you a cool, little code snippet, that works, but i can point you to the right direction:  
You will probably want to use a Service for this: [https://developer.android.com/guide/components/services](https://developer.android.com/guide/components/services). Services need some time to read in, but after that you should be able to achieve to have a client in the background.

Best regards,

Daniel

---

<div class="post-metadata">

**Author:** ![kalpana](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kalpana](https://community.hivemq.com/u/kalpana)\
**Post date:** [November 23, 2020, 7:09am UTC](https://community.hivemq.com/t/mqtt-client-connection-alive-even-when-app-is-closed/366/3 "2020-11-23T07:09:42Z")

</div>

Hey,  
I am currently using HIVEMQTT protocol in my android app and I am able to receive messages when my app is open or when it is running in the background however when i close my app completely the connection is lost and I am not longer able to receive messages. I need to be able to still recieve messages as I want the message to trigger an activity in my app to launch.

---

<div class="post-metadata">

**Author:** ![tomerpetel](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@tomerpetel](https://community.hivemq.com/u/tomerpetel)\
**Post date:** [July 2, 2021, 2:09pm UTC](https://community.hivemq.com/t/mqtt-client-connection-alive-even-when-app-is-closed/366/4 "2021-07-02T14:09:11Z")

</div>

I guess that you’ll need a foreground service in order to keep it alive while app is closed. See more here:

> **[Foreground services  |  Android Developers](https://developer.android.com/guide/components/foreground-services)**
