I posted this in the Mattermost Town Square, but was encouraged to post the question here also:
Good afternoon. I'm wondering if CiviCRM can produce a "changes stream" or fire off a webhook when records change? I've been digging around the docs and the closest thing that I can find to what I'm looking for is the hook_civicrm_pre. Specifically, I'd like to sync records from CiviCRM to another system, and I'm looking for a way to send a notification from CiviCRM to the other system saying "Hey, something changed over here!"
Joe Murray replied:
You probably want https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postCommit/ rather than hook_civicrm_pre. I would check whether it is called for all of the use cases/objects you are trying to replicate to the other system.
Note that these hooks may be noisy, ie called when a write operation is occurring but no change is being made to any of the data - ie the pre and post write record in db is identical. Depending on criteria you're trying to optimize for synchronization, it might be worth it to do the work to filter down to things that are real changes not just potential ones.
Alax Dixon added:
There's also a hook_civicrm_post, which is more likely the right tool, but you might do better to narrow your scope to only trying to sync specific types of entities and use whatever entity-specific hook there might be for it (e.g. contacts).
And GHarris added:
For some of the more obvious things, the CiviRules extension may be a lot simpler to implement.
I'm still working on this and would appreciate input from anyone who's worked on syncing CiviCRM with external systems. Thanks!