8 hours has been found to be the optimal time for a longer duration session. Anything longer should be handled differently, using the API to instruct connections and disconnects after the 8-hour mark. Keep reading to learn the best practices for implementing such actions using pseudo code.
Issues that occur with 8+ hour sessions
Solutions and best practices
These are two alternative solutions here that we usually suggest in such cases:
- Keep your sessions short(er)
- Apply our persistent connection mechanism
We will leave the “keep session shorter” option to you to implement (you will need to close and reopen the WebRTC peer connections in your application for that). If you’d rather not take that route, then read on to see how you can use persistent connections instead.
Persistent connections for long sessions: or pseudo code
Persistent connection is a mechanism that is used by call centers to hold a single WebRTC connection while “splitting” it internally to multiple logical calls from users. The same concept can be applied by watchRTC to also split a long session into shorter ones.
- Keep track of sessions
- Connect and disconnect every 8 hours (or a different time frame of your choosing)
- Modify roomID for each new connection
let sessionsIterator = 1;
watchRTC.persistentStart(roomId, peerId);
setInterval(
() => {
watchRTC.persistentEnd();
sessionsIterator++;
watchRTC.persistentStart(roomId + '-' + sessionsIterator, peerId);
},
60000 * 60 * 8 // 8h
);
-
let sessionsIterator = 1;
This line initializes a variable sessionsIterator to 1. This variable is used to keep track of sessions.
-
watchRTC.persistentStart(roomId, peerId);
This function is being used to start a persistent session with the roomId and peerId parameters.
-
setInterval(() => {...}, 60000 * 60 * 8’
This sets up a recurring function that will execute every 8 hours (60000 milliseconds * 60 seconds * 8 hours).
Inside this function:- watchRTC.persistentEnd(); This function is used to end the current persistent session.
- sessionsIterator++; This increments the sessionsIterator variable to keep track of the number of sessions.
- watchRTC.persistentStart(roomId + '-' + sessionsIterator, peerId); This calls persistentStart() function again, but this time with a modified roomId. It combines the current ‘roomId’ with the current ‘sessionsIterator’, separated by a hyphen to differentiate sessions.
Benefits of adding this pseudo code
By using pseudo code which instructs to disconnect and create a new user with an updated peer name every 8 hours, users can avoid all of the issues mentioned above.