Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 way syncing with Google Calendar/Outlook

I am using FullCalendar in my application to display events created via our own application.

I have an add/edit form for creating/updating events. These events are stored in the db used by application.

I need to move further from this and need to sync Google and Outlook calendars into my calendar. This should be 2 way syncing i.e

If I create/edit/update an event in my calendar it should be created/edited/deleted in Google/Outlook calendars.

It should be vice-versa too.

If I am doing some change in Google/Outlook calendars it should be visible in my calendar.

I would like your thoughts on implementing this:

  1. Should I fetch all the events of Google/Outlook and import them into my db and then display them in my calendar view. Is this even technically possible? i.e importing the entire set of events of a channel to my db.

  2. Should I just do a GET via Google/Outlook API to fetch the events for a particular view where I am right now in my calendar (I will be having start data and end date of my calendar view) and just show them to my calendar (i.e I am not storing those external events in my db). If a user wants to do any change in the events that should be updated directly to Google/Outlook calendars via their API calls of create/update and delete.

What should be the best approach?

Edit:

I went to https://calendar.sunrise.am/ (one of the calendar sync web app) and noticed

enter image description here

i.e they are allowing many different applications calendars/tasks to be synced into their calendar.

Seeing all that I feel that storing all the events of all those applications into our own application's db is not feasible. If any change is done to those events via my application I should call the API of those applications to make that change into their application (Google calendar, Outlook etc).

Whats your thoughts?

like image 785
Raghav Avatar asked Jun 17 '16 16:06

Raghav


People also ask

Can you link 2 emails to Google Calendar?

Step 1: From the left panel, under My calendars, click on the “three dots” of the calendar you want to share. Step 2: Click on the Settings and Sharing option. Step 3: Select “Share with specific people.” Step 4: Select “Add People”, write their email address, and set the permissions.


1 Answers

To be able to create reliable sync solution you need several things. Most important is that the other party (google calendar and outlook in this case) should cooperate with you and provide an api to perform incremental synchronization. I didn't look at Outlook, but Google Calendar api provides you all you need.

First to answer your question - yes, you need to fetch all events (you may skip events in the past though) and store them in your own database. Always making a query to all external sources (plus to your own database) is slow, makes synchronization much harder and limits you quite a lot, because you cannot for example filter or search events at multiple sources easily. Below I will assumed we are working with Google Calendar only, hopefully Outlook is similar (but I did not check).

So checklist of what you need:

  1. Your own database with events, where event table has several important metadata columns: Created\Updated (time when event was last created or updated, not related to the date of event itself), Source (where this event came from, like Google Calendar, Outlook or your own app), UpdatedAtSource (source where this event was last modified), EventID (unique identifier of event - important to have that to prevent duplicates in certain cases).

  2. Initially fetch all events from target provider and store them in your database. Here is a reference to the structure of Google Calendar event and you see that all required metadata fields (created,updated, id) are present there.

  3. Now you need to watch for new events coming from provider. You can either do this by polling (periodically checking if there are new events) or by making provider push events to you. Google Calendar supports both options. Here is a link describing how to implement push notifications and here is the link describing how to get only new events, that is events you (your application) didn't see before. Note that you don't need to fetch whole list every time, nor do you need to provide some filter options (like "give me all events created after 2016-06-21"). All this would be unreliable, but Google Calendar developers know how to make good sync api, so they took care of that for you. Just grab and store provided nextSyncToken and use it to make future requests. If you use push notifications - always also periodically poll events, but not often (like one every several hours). Push notifications are not 100% reliable and some can be missed - you need to handle those using that nextSyncToken api.

  4. Push changes made by your own application to target providers. But, do not do this immediatly when the change itself is made. Instead use background process which pushes changes for each user+provider pair one by one. There will be network failures, there will be conflicts, so you have to push changes sequentially, not in parallel (again, sequentially for every user + provider pair, not globally). Store timestamp of last successfully pushed change (again, for every user + provider) and if process has been interrupted - you know where to start over.
  5. I will not cover that here much, but you will conflicts - that is when user modified same event in multiple sources. However, if you use push notifications - conflicts will be very rare. Still you have to plan for them at least in user interface. If you detected unresolvable conflict - pause synchronization process and ask user how to resolve it.

So you see that there is some work to do, but in the end you will make small number of requests and fetch small amount of data with each request to provider, and your users will be happy to see new events from their Google Calendar\Outlook in your application immediatly (and visa versa).

like image 160
Evk Avatar answered Nov 15 '22 19:11

Evk