There is a session refresh feature currently in preview which should solve your problem without the need of developing something custom.
https://learn.microsoft.com/en-us/dynamics365/customer-service/administer/enable-session-restore