How to detect mobile device disconnected from sync server

Hi
I am new to couch database. In the project, we run the couch sync gateway in a windows machine (local only) and have ios mobile app. I require to detect when the ios app goes offline and to set a flag in the database from the windows app. Is there any way by which I can know the mobile device gone offline.
I am not using background sync. And know I got a few seconds to set database changes when the mobile app goes offline. But no way if the app crashes
Thanks in advance
I am using Sync gateway 2.6.1 in windows
And CouchbaseLite-Swift version 2.6.3 (Community Edition)

I require to detect when the ios app goes offline and to set a flag in the database from the windows app.

What windows app are you referring to ? Is it the Sync Gateway?
What do you mean by “iOS app goes offline” ? App can be terminated or pushed to background but offline isn’t a valid iOS app life cycle state. Did you mean device goes offline ?

If you are looking to pull a document from Sync Gateway before the iOS app is closed or pushed to the background, then you must react to app life cycle events and schedule a background task to do a one-shot pull replication.

If you want to know if you have lost connectivity with Sync Gateway, you can monitor the replicator status

But from your subject line of your question, I think my assumptions of what you are looking for may be wrong. So please describe the use case step by step.

I require to detect when the ios app goes offline and to set a flag in the database from the windows app.

We don’t have any direct support for this; Couchbase Mobile works with persistent data and doesn’t really present transient information like who’s connected.

I think the best way to do this would be to put a reverse proxy like nginx in front of Sync Gateway. Then you could add logic on the proxy to detect when WebSocket connections are opened and closed, and update state data somewhere.

Thanks Priya for replay.

We have a windows app in the server which connects to sync gateway.

I used offline which includes inactive, background suspended and not running after a crash… in the crashed case I don’t have a way to execute code. The device I meant is iOS

In other words, I want to know iOS device goes offline in my windows application

Thanks Jens,
I have to check how to implement reverse proxy…

On further investigation, I found the following

2019-11-22T09:19:24.149+05:30 [INF] HTTP: c:[6fa7d00d] #001:    --> BLIP+WebSocket connection closed
2019-11-22T09:19:24.149+05:30 [INF] Changes: c:[6fa7d00d] MultiChangesFeed done  

in the sync log. So my question is if I trail the log and monitor disconnect will it meet my requirement?
The challenge will be to find out the device from the id (

HTTP: c:[6fa7d00d] #001:

) in the log.
If I try to add some info to CouchDB just after a connection can I Identify device Id from a previous log?

2019-11-22T09:19:14.113+05:30 [INF] WS: c:[6fa7d00d] Start BLIP/Websocket handler

I feel this is not a good method. but like to know your opinion

I thought of that but decided not to mention it, because of course it’s fragile — the wording of log messages could change without notice. You can do it, as long as you’re willing to accept that your system could break after updating Sync Gateway and you’d have to update the regex you’re using to sniff the log.

The challenge will be to find out the device from the id

The username should appear in the HTTP-related log messages, I think it’s parenthesized at the end of the message. I don’t see it in the lines you posted, but look in earlier lines for the same connection.

Thanks Jens,
The user name didn’t appear in the log because mobile device connects to sync gateway as guest. Connecting with username is a good idea…