Two peers are connected - host and client
Client gets offline and iceConnectionState - 'disconnected' on host is triggered after about 3-7 seconds
Why is there a delay ? and how to remove that delay?
I just wanted get online status of user in realtime
Peer connection gets closed when there's no data coming for several seconds, hence the delay. There's no Web API to configure this timeout.
I see three ways how you might reduce the delay:
Send keepalives between peers via datachannel. If there were no keepalive for n milliseconds, client can be considered disconnected. Then the server might close the connection or display "pending" user status. Some timeout is still needed, but it can be shorter than the default.
If client always sends the video, you might use getStats() to detect when there's no new packets coming. It's the same as 1, but using video packets instead of keepalive messages. See this answer.
Send a message to server when client is about to disconnect. For example, you could send a "goodbye" message to WebRTC datachannel in window.onbeforeunload
callback. The drawback is that if the client goes offline without closing the page you still have to wait for whatever is the default timeout in your browser.