Try our conversational search powered by Generative AI!

Tobias Nilsson
Mar 3, 2016
  2510
(2 votes)

How to listen to remote Commerce events

This will be a quick tips for people who want to listen to external Commerce events. I have a perculiar situation where I via the Service API import my products, but the service API isn't hosted on the same site/server as my site, but on a separate one instead. This means that I can't listen to local events like I would normally and will have to listen to remote events. But is there such events for Commerce? I know that Episerver sends remote events for cache invalidations, so maybe they send events for Commerce event too.

To listen to local Commerce events you can create your own implementation of CatalogEventListenerBase and one of the out of the box implementation of this abstract class is the CatalogEventBroadcaster. This class will take the local event and send out remote events that we can listen to! So to solve my problem I implemented the following in a IConfigurableModule.

	[ModuleDependency(typeof(ServiceContainerInitialization))]
	public class CatalogEventListener : IConfigurableModule
	{
		public void Initialize(EpiserverCommerce.Framework.Initialization.InitializationEngine context)
		{
			Event ev = Event.Get(CatalogEventBroadcaster.CommerceProductUpdated);
			ev.Raised += Ev_Raised;
		}

		private void Ev_Raised(object sender, EpiserverCommerce.Events.EventNotificationEventArgs e)
		{
			if (!(e.Param is Byte[])) return;
			var eventArgs = DeSerialize(e.Param as Byte[]);
			// Care only about catalog entry events
			var catalogEvents = eventArgs as CatalogContentUpdateEventArgs;
			if (catalogEvents == null || !catalogEvents.CatalogEntryIds.Any()) return;

			switch (catalogEvents.EventType)
			{
				case Mediachase.Commerce.Catalog.Events.CatalogEventBroadcaster.CatalogEntryUpdatedEventType:
					catalogEvents.CatalogEntryIds.ForEach(entryId => UpdateIndex(entryId));
					break;
				default:
					break;
			}
		}

		private static EventArgs DeSerialize(byte[] buffer)
		{
			var formatter = new BinaryFormatter();
			using (var stream = new MemoryStream(buffer))
			{
				return formatter.Deserialize(stream) as EventArgs;
			}
		}		
	}

Big thanks to Quan Mai for helping me find the right class!

Mar 03, 2016

Comments

Vincent
Vincent Jul 14, 2016 02:43 AM

Thanks, Toni. I recently experienced the same issue, Quan pointed me to your blog post. 

Please login to comment.
Latest blogs
Anonymous Tracking Across Devices with Optimizely ODP

An article by Lead Integration Developer, Daniel Copping In this article, I’ll describe how you can use the Optimizely Data Platform (ODP) to...

Daniel Copping | Apr 30, 2024 | Syndicated blog

Optimizely Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024

Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024