# ParseSwift SDK: Observe LiveQuery WebSocket status

**URL:** <https://community.parseplatform.org/t/parseswift-sdk-observe-livequery-websocket-status/1739>\
**Category:** ParseSwift SDK\
**Created:** [June 7, 2021, 1:20pm UTC](https://community.parseplatform.org/t/parseswift-sdk-observe-livequery-websocket-status/1739 "2021-06-07T13:20:18Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![lsmilek1](https://community.parseplatform.org/user_avatar/community.parseplatform.org/lsmilek1/32/629_2.png) [@lsmilek1](https://community.parseplatform.org/u/lsmilek1)\
**Post date:** [June 10, 2021, 8:57am UTC](https://community.parseplatform.org/t/parseswift-sdk-observe-livequery-websocket-status/1739/7 "2021-06-10T08:57:02Z")

</div>

Whet the application gets terminated in the background by the iOs, then there is no reason to keep any connections open, am I right? As there would be app launch again, where the application would start the connections again.

So it somehow feels correct to call

```auto
ParseLiveQuery.getDefault()?.close()

```

in

```auto
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>)

```

I am just not sure if that is enough to close everything as I see after trying it in `sceneDidEnterBackground`

> testing LiveQuery isConnected: Optional(false)  
> testing LiveQuery isConnecting: Optional(false)  
> **testing LiveQuery isSocketEstablished: Optional(true)** ← this has any effect or is the connection closed on server side with no hanging resources?  
> **testing LiveQuery isSubscribed: Optional(true)** ← this is probably due to cached queries and has to effect  
> testing LiveQuery isPendingSubscription: Optional(false)

Of do I again understand it wrong and the application actually do not open a new connection on the next launch if there is previous connection still available? I am confused if this might happen as the application gets terminated.

And as I tried also without terminating application, just putting in background and back active, I can’t somehow re-open the connection:

1. **ParseLiveQuery.getDefault()?.close()**
2. **testing LiveQuery isConnected: Optional(false)**
3. **ParseLiveQuery.getDefault().open(completion: { error in  
print(“error while opening webSocket: (error?.localizedDescription)”)  
})**
4. **testing LiveQuery isConnected: Optional(false)**

`isConnected` stays false even the `.open()` do not throw any error

I was thinking about following strategy to handle bad connectivity:

- on app launch fetch all data to get up-to-date and subscribe LiveQuery
- to detect bad connectivity
  1. on 3x failed `.save()` of `.fetch()` inform user with manual “retry” button and when there is success after manual trigger later the application should check if LiveQuery is still life. If not, try to restart like a fresh app launch
  2. sending `Date()` value in `.save()` context and reject the save in `beforeSave` cloud code if object’s `updatedAt` on server is newer than value of the pending save → this prevents outdated saves and inform client that there was some updates missed → if data were missed, client app would again fetch and restart LiveQuery

But here I am not sure how to restart or close LiveQuery in a clean way as the above `isConnected` stays `false` and `isSocketEstablished` stays `true`

What I am trying to achieve is to primary not drain server resources through some zombie webSockets opened, because outdated state on device is not critical as the server can reject outdated save operation

---

_[View the full topic](https://community.parseplatform.org/t/parseswift-sdk-observe-livequery-websocket-status/1739)._
