2 way syncing with Google Calendar/Outlook

前端 未结 1 921
栀梦
栀梦 2021-02-03 11:41

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 stor

1条回答
  •  生来不讨喜
    2021-02-03 11:49

    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).

    0 讨论(0)
提交回复
热议问题