Try our conversational search powered by Generative AI!

Tobias Nilsson
Mar 3, 2016
  2541
(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
Integrating HubSpot CRM without the MA Connector

Have HubSpot CRM? Want to push user data into it from Optimizely? Don’t have any personalisation requirements with that data? Don’t want to pay $80...

Matt Pallatt | Jun 27, 2024

Keeping the website secure by updating external packages

Did you see the latest warning from Optimizely to update this package with a critical security warning? https://world.optimizely.com/documentation/...

Daniel Ovaska | Jun 27, 2024

Optimizely CMS image anonymization now available for Linux!

The famous image anonymization add-on for Optimizely CMS, with at least 5 downloads, is now finally available for use on Linux. Supports simultaneo...

Tomas Hensrud Gulla | Jun 25, 2024 | Syndicated blog

Remove a segment from the URL in CMS 12

Problem : I have created thousands of pages dynamically using schedule jobs with different templates (e.g. one column, two columns, etc..) and stor...

Sanjay Kumar | Jun 21, 2024