What you are asking is actually incredibly complicated.
I have spent a good year of my own time trying to solve this problem and have published my work in the SyncIt project which is at http://forbesmyester.github.io/SyncIt/ .
There is:
- A local version control system https://github.com/forbesmyester/SyncIt
- A server component https://github.com/forbesmyester/syncit-server
- A connection monitoring component https://github.com/forbesmyester/SyncItControl
- And a basic bootstrap project that will give you a super basic to do list like project which you who be able to use as a basis for your project... https://github.com/forbesmyester/SyncItBootstrap
I suggest that you start reading about the local version control component and then move onto the connection monitoring component.