Skip to content

EspoCRM – Open Source CRM Application

Event dispatcher

Yurii3640cc003766
diff --git a/application/Espo/Binding.php b/application/Espo/Binding.php
index 127425d96d..a6b2a78823 100644
--- a/application/Espo/Binding.php
+++ b/application/Espo/Binding.php
@@ -262,6 +262,16 @@ class Binding implements BindingProcessor
'Espo\\Core\\Utils\\System\\SystemState',
'systemState'
);
+
+ $binder->bindService(
+ 'Espo\\Core\\Utils\\Event\\Configuration',
+ 'eventDispatcherConfiguration',
+ );
+
+ $binder->bindService(
+ 'Espo\\Core\\Utils\\Event\\EventDispatcherTransport',
+ 'eventDispatcherTransport'
+ );
}
private function bindCore(Binder $binder): void
diff --git a/application/Espo/Core/Job/Processing/Util/ExitPolicy.php b/application/Espo/Core/Job/Processing/Util/ExitPolicy.php
index b656ad153a..68a8d3793d 100644
--- a/application/Espo/Core/Job/Processing/Util/ExitPolicy.php
+++ b/application/Espo/Core/Job/Processing/Util/ExitPolicy.php
@@ -31,6 +31,7 @@ namespace Espo\Core\Job\Processing\Util;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Config\StateConfig;
+use Espo\Core\Utils\Event\EventDispatcherTransport;
class ExitPolicy
{
@@ -39,12 +40,15 @@ class ExitPolicy
public function __construct(
private StateConfig $stateConfig,
private Config\StateConfigDirect $stateConfigDirect,
+ private EventDispatcherTransport $eventDispatcherTransport,
) {
$this->cacheTimestamp = $this->stateConfig->getCacheTimestamp();
}
public function toExit(): bool
{
- return $this->cacheTimestamp !== $this->stateConfigDirect->getCacheTimestamp();
+ return
+ $this->cacheTimestamp !== $this->stateConfigDirect->getCacheTimestamp() ||
+ $this->eventDispatcherTransport->shouldReconnect();
}
}
diff --git a/application/Espo/Core/Job/Processing/WorkerDaemon.php b/application/Espo/Core/Job/Processing/WorkerDaemon.php
index fb3ee3b0d4..e079ffe6ca 100644
--- a/application/Espo/Core/Job/Processing/WorkerDaemon.php
+++ b/application/Espo/Core/Job/Processing/WorkerDaemon.php
@@ -31,6 +31,7 @@ namespace Espo\Core\Job\Processing;
use Espo\Core\Job\Processing\Consumer\Params;
use Espo\Core\Job\Processing\Util\ExitSetup;
+use Espo\Core\Utils\Event\Configuration;
/**
* @since 10.1.0
@@ -41,6 +42,7 @@ class WorkerDaemon
public function __construct(
private Consumer $consumer,
private ExitSetup $exitSetup,
+ private Configuration $eventConfiguration,
) {}
public function run(WorkerDaemon\Params $params): void
@@ -49,6 +51,8 @@ class WorkerDaemon
$this->setupExit();
+ $this->eventConfiguration->setSubscribeToCrossInstanceEvents(true);
+
$this->consumer->start($consumerParams);
}
diff --git a/application/Espo/Core/Loaders/EventDispatcher.php b/application/Espo/Core/Loaders/EventDispatcher.php
new file mode 100644
index 0000000000..703166f0cd
--- /dev/null
+++ b/application/Espo/Core/Loaders/EventDispatcher.php
@@ -0,0 +1,55 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Loaders;
+
+use Espo\Core\Binding\BindingContainerBuilder;
+use Espo\Core\Container\Loader;
+use Espo\Core\InjectableFactory;
+use Espo\Core\Utils\Event\EventDispatcher as EventDispatcherService;
+use Espo\Core\Utils\Event\OriginProvider;
+
+class EventDispatcher implements Loader
+{
+ public function __construct(
+ private InjectableFactory $injectableFactory,
+ ) {}
+
+ public function load(): EventDispatcherService
+ {
+ $originProvider = $this->injectableFactory->create(OriginProvider::class);
+
+ return $this->injectableFactory->createWithBinding(
+ EventDispatcherService::class,
+ BindingContainerBuilder::create()
+ ->bindInstance(OriginProvider::class, $originProvider)
+ ->build()
+ );
+ }
+}
diff --git a/application/Espo/Core/Utils/Event/BypassEventDispatcherTransport.php b/application/Espo/Core/Utils/Event/BypassEventDispatcherTransport.php
new file mode 100644
index 0000000000..9e635da90e
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/BypassEventDispatcherTransport.php
@@ -0,0 +1,46 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use Closure;
+
+class BypassEventDispatcherTransport implements EventDispatcherTransport
+{
+ public function subscribe(Closure $callback): void
+ {}
+
+ public function dispatch(Envelope $envelope): void
+ {}
+
+ public function shouldReconnect(): bool
+ {
+ return false;
+ }
+}
diff --git a/application/Espo/Core/Utils/Event/Configuration.php b/application/Espo/Core/Utils/Event/Configuration.php
new file mode 100644
index 0000000000..fb83e9d720
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/Configuration.php
@@ -0,0 +1,55 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+/**
+ * @internal
+ */
+class Configuration
+{
+ public function __construct(
+ private bool $subscribeToCrossInstanceEvents = false,
+ ) {}
+
+ public function subscribeToCrossInstanceEvents(): bool
+ {
+ return $this->subscribeToCrossInstanceEvents;
+ }
+
+ /**
+ * @internal
+ */
+ public function setSubscribeToCrossInstanceEvents(bool $value): self
+ {
+ $this->subscribeToCrossInstanceEvents = $value;
+
+ return $this;
+ }
+}
diff --git a/application/Espo/Core/Utils/Event/Context.php b/application/Espo/Core/Utils/Event/Context.php
new file mode 100644
index 0000000000..59471cddc8
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/Context.php
@@ -0,0 +1,44 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+/**
+ * @since 10.1.0
+ */
+readonly class Context
+{
+ /**
+ * @internal
+ */
+ public function __construct(
+ public bool $isLocal,
+ public string $origin,
+ ) {}
+}
diff --git a/application/Espo/Core/Utils/Event/CrossInstanceEvent.php b/application/Espo/Core/Utils/Event/CrossInstanceEvent.php
new file mode 100644
index 0000000000..63ab30a0d3
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/CrossInstanceEvent.php
@@ -0,0 +1,39 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use stdClass;
+
+interface CrossInstanceEvent extends Event
+{
+ public static function fromRaw(stdClass $payload): static;
+
+ public function toRaw(): stdClass;
+}
diff --git a/application/Espo/Core/Utils/Event/CrossInstanceEventDispatcher.php b/application/Espo/Core/Utils/Event/CrossInstanceEventDispatcher.php
new file mode 100644
index 0000000000..70e7cb5f9d
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/CrossInstanceEventDispatcher.php
@@ -0,0 +1,138 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use Closure;
+use RuntimeException;
+use Throwable;
+
+class CrossInstanceEventDispatcher
+{
+ /**
+ * @var array<class-string<Event>, (Closure(CrossInstanceEvent, Context): void)[]>
+ */
+ private array $callbacks = [];
+
+ private bool $isTransportSubscribed = false;
+
+ public function __construct(
+ private EventDispatcherTransport $transport,
+ private OriginProvider $originProvider,
+ ) {}
+
+ /**
+ * @param class-string<CrossInstanceEvent> $className
+ * @param Closure(CrossInstanceEvent, Context): void $callback
+ */
+ public function subscribe(string $className, Closure $callback): void
+ {
+ $this->ensureSubscribeTransport();
+
+ $this->callbacks[$className] ??= [];
+ $this->callbacks[$className][] = $callback;
+ }
+
+ /**
+ * @param class-string<CrossInstanceEvent> $className
+ * @param Closure(CrossInstanceEvent, Context): void $callback
+ */
+ public function unsubscribe(string $className, Closure $callback): void
+ {
+ if (!array_key_exists($className, $this->callbacks)) {
+ return;
+ }
+
+ $list = &$this->callbacks[$className];
+
+ $index = array_search($callback, $list);
+
+ if ($index !== false) {
+ unset($list[$index]);
+
+ $list = array_values($list);
+ }
+ }
+
+ public function dispatch(CrossInstanceEvent $event): void
+ {
+ $envelope = new Envelope(
+ eventClassName: $event::class,
+ payload: $event->toRaw(),
+ origin: $this->originProvider->get(),
+ );
+
+ $this->transport->dispatch($envelope);
+ }
+
+ private function ensureSubscribeTransport(): void
+ {
+ if ($this->isTransportSubscribed) {
+ return;
+ }
+
+ $this->transport->subscribe(fn (Envelope $envelope) => $this->transportCallback($envelope));
+
+ $this->isTransportSubscribed = true;
+ }
+
+ private function transportCallback(Envelope $envelope): void
+ {
+ if ($envelope->origin === $this->originProvider->get()) {
+ return;
+ }
+
+ $className = $envelope->eventClassName;
+
+ if (!is_subclass_of($className, CrossInstanceEvent::class)) {
+ throw new RuntimeException("Non-valid event class name.");
+ }
+
+ $callbacks = $this->callbacks[$className] ?? [];
+
+ if ($callbacks === []) {
+ return;
+ }
+
+ try {
+ $event = $className::fromRaw($envelope->payload);
+ } catch (Throwable $e) {
+ throw new RuntimeException("Could not hydrate event '$className'.", previous: $e);
+ }
+
+ $context = new Context(
+ isLocal: false,
+ origin: $envelope->origin,
+ );
+
+ foreach ($callbacks as $callback) {
+ $callback($event, $context);
+ }
+ }
+}
diff --git a/application/Espo/Core/Utils/Event/Envelope.php b/application/Espo/Core/Utils/Event/Envelope.php
new file mode 100644
index 0000000000..7d2c08dfd6
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/Envelope.php
@@ -0,0 +1,44 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use stdClass;
+
+readonly class Envelope
+{
+ /**
+ * @param class-string<CrossInstanceEvent> $eventClassName
+ */
+ public function __construct(
+ public string $eventClassName,
+ public stdClass $payload,
+ public string $origin,
+ ) {}
+}
diff --git a/application/Espo/Core/Utils/Event/Event.php b/application/Espo/Core/Utils/Event/Event.php
new file mode 100644
index 0000000000..d80eb1b0c2
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/Event.php
@@ -0,0 +1,36 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+/**
+ * @since 10.1.0
+ */
+interface Event
+{}
diff --git a/application/Espo/Core/Utils/Event/EventDispatcher.php b/application/Espo/Core/Utils/Event/EventDispatcher.php
new file mode 100644
index 0000000000..ba8e52fd72
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/EventDispatcher.php
@@ -0,0 +1,112 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use Closure;
+
+/**
+ * @since 10.1.0
+ */
+class EventDispatcher
+{
+ /**
+ * @var array<class-string<Event>, (Closure(Event, Context): void)[]>
+ */
+ private array $callbacks = [];
+
+ public function __construct(
+ private OriginProvider $originProvider,
+ private CrossInstanceEventDispatcher $crossInstanceDispatcher,
+ private Configuration $configuration,
+ ) {}
+
+ /**
+ * @param class-string<Event> $className
+ * @param Closure(Event, Context): void $callback
+ */
+ public function subscribe(string $className, Closure $callback): void
+ {
+ $this->callbacks[$className] ??= [];
+ $this->callbacks[$className][] = $callback;
+
+ if (
+ $this->configuration->subscribeToCrossInstanceEvents() &&
+ is_subclass_of($className, CrossInstanceEvent::class)
+ ) {
+ $this->crossInstanceDispatcher->subscribe($className, $callback);
+ }
+ }
+
+ /**
+ * @param class-string<Event> $className
+ * @param Closure(Event, Context): void $callback
+ */
+ public function unsubscribe(string $className, Closure $callback): void
+ {
+ if (!array_key_exists($className, $this->callbacks)) {
+ return;
+ }
+
+ $list = &$this->callbacks[$className];
+
+ $index = array_search($callback, $list);
+
+ if ($index !== false) {
+ unset($list[$index]);
+
+ $list = array_values($list);
+ }
+
+ if (
+ $this->configuration->subscribeToCrossInstanceEvents() &&
+ is_subclass_of($className, CrossInstanceEvent::class)
+ ) {
+ $this->crossInstanceDispatcher->unsubscribe($className, $callback);
+ }
+ }
+
+ public function dispatch(Event $event): void
+ {
+ $callbacks = $this->callbacks[$event::class] ?? [];
+
+ $localContext = new Context(
+ isLocal: true,
+ origin: $this->originProvider->get(),
+ );
+
+ foreach ($callbacks as $callback) {
+ $callback($event, $localContext);
+ }
+
+ if ($event instanceof CrossInstanceEvent) {
+ $this->crossInstanceDispatcher->dispatch($event);
+ }
+ }
+}
diff --git a/application/Espo/Core/Utils/Event/EventDispatcherTransport.php b/application/Espo/Core/Utils/Event/EventDispatcherTransport.php
new file mode 100644
index 0000000000..4516c6b988
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/EventDispatcherTransport.php
@@ -0,0 +1,47 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use Closure;
+
+/**
+ * @since 10.1.0
+ */
+interface EventDispatcherTransport
+{
+ /**
+ * @param Closure(Envelope): void $callback
+ */
+ public function subscribe(Closure $callback): void;
+
+ public function dispatch(Envelope $envelope): void;
+
+ public function shouldReconnect(): bool;
+}
diff --git a/application/Espo/Core/Utils/Event/EventDispatcherTransportLoader.php b/application/Espo/Core/Utils/Event/EventDispatcherTransportLoader.php
new file mode 100644
index 0000000000..4e4e7fba3b
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/EventDispatcherTransportLoader.php
@@ -0,0 +1,48 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use Espo\Core\Container\Loader;
+use Espo\Core\InjectableFactory;
+
+/**
+ * @noinspection PhpUnused
+ */
+class EventDispatcherTransportLoader implements Loader
+{
+ public function __construct(
+ private InjectableFactory $injectableFactory,
+ ) {}
+
+ public function load(): EventDispatcherTransport
+ {
+ return $this->injectableFactory->create(BypassEventDispatcherTransport::class);
+ }
+}
diff --git a/application/Espo/Core/Utils/Event/OriginProvider.php b/application/Espo/Core/Utils/Event/OriginProvider.php
new file mode 100644
index 0000000000..e98297ecf4
--- /dev/null
+++ b/application/Espo/Core/Utils/Event/OriginProvider.php
@@ -0,0 +1,47 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\Event;
+
+use Espo\Core\Utils\Util;
+
+/**
+ * @since 10.1.0
+ */
+class OriginProvider
+{
+ private ?string $value = null;
+
+ public function get(): string
+ {
+ $this->value ??= Util::generateId();
+
+ return $this->value;
+ }
+}
diff --git a/application/Espo/Resources/metadata/app/containerServices.json b/application/Espo/Resources/metadata/app/containerServices.json
index 8af3f9a59b..172a4a2eaf 100644
--- a/application/Espo/Resources/metadata/app/containerServices.json
+++ b/application/Espo/Resources/metadata/app/containerServices.json
@@ -91,5 +91,11 @@
},
"session": {
"className": "Espo\\Core\\Session\\DefaultSession"
+ },
+ "eventDispatcherConfiguration": {
+ "className": "Espo\\Core\\Utils\\Event\\Configuration"
+ },
+ "eventDispatcherTransport": {
+ "loaderClassName": "Espo\\Core\\Utils\\Event\\EventDispatcherTransportLoader"
}
}
diff --git a/tests/unit/Espo/Core/Utils/Event/CrossInstanceEventDispatcherTest.php b/tests/unit/Espo/Core/Utils/Event/CrossInstanceEventDispatcherTest.php
new file mode 100644
index 0000000000..b8e9cb80bd
--- /dev/null
+++ b/tests/unit/Espo/Core/Utils/Event/CrossInstanceEventDispatcherTest.php
@@ -0,0 +1,79 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Utils\Event;
+
+use Espo\Core\Utils\Event\Context;
+use Espo\Core\Utils\Event\CrossInstanceEventDispatcher;
+use Espo\Core\Utils\Event\OriginProvider;
+use PHPUnit\Framework\TestCase;
+
+class CrossInstanceEventDispatcherTest extends TestCase
+{
+ public function testDispatching(): void
+ {
+ $originProvider = $this->getOriginProvider();
+
+ $transport = new TestTransport();
+
+ $dispatcher = new CrossInstanceEventDispatcher(
+ transport: $transport,
+ originProvider: $originProvider,
+ );
+
+ $value = false;
+
+ $callback1 = function (TestCiEvent1 $event, Context $context) use (&$value) {
+ if (!$context->isLocal && $event->value === 'hello') {
+ $value = true;
+ }
+ };
+
+ $callback2 = function () use (&$value) {
+ $value = false;
+ };
+
+ $dispatcher->subscribe(TestCiEvent1::class, $callback1);
+ $dispatcher->subscribe(TestCiEvent2::class, $callback2);
+
+ $transport->dispatchForTest(TestCiEvent1::class, (object) ['value' => 'hello']);
+
+ $this->assertTrue($value);
+ }
+
+ private function getOriginProvider(): OriginProvider
+ {
+ $originProvider = $this->createMock(OriginProvider::class);
+
+ $originProvider->method('get')
+ ->willReturn('test-id');
+
+ return $originProvider;
+ }
+}
diff --git a/tests/unit/Espo/Core/Utils/Event/EventDispatcherTest.php b/tests/unit/Espo/Core/Utils/Event/EventDispatcherTest.php
new file mode 100644
index 0000000000..e8c2407259
--- /dev/null
+++ b/tests/unit/Espo/Core/Utils/Event/EventDispatcherTest.php
@@ -0,0 +1,95 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Utils\Event;
+
+use Espo\Core\Utils\Event\Configuration;
+use Espo\Core\Utils\Event\Context;
+use Espo\Core\Utils\Event\CrossInstanceEventDispatcher;
+use Espo\Core\Utils\Event\EventDispatcher;
+use Espo\Core\Utils\Event\OriginProvider;
+use PHPUnit\Framework\TestCase;
+
+class EventDispatcherTest extends TestCase
+{
+ public function testLocalDispatching() : void
+ {
+ $originProvider = $this->getOriginProvider();
+ $crossInstanceDispatcher = $this->createMock(CrossInstanceEventDispatcher::class);
+
+ $dispatcher = new EventDispatcher(
+ originProvider: $originProvider,
+ crossInstanceDispatcher: $crossInstanceDispatcher,
+ configuration: $this->createMock(Configuration::class),
+ );
+
+ //
+
+ $value = false;
+
+ $callback1 = function (TestEvent1 $event, Context $context) use (&$value, $originProvider) {
+ if ($context->isLocal && $context->origin === $originProvider->get()) {
+ $value = true;
+ }
+ };
+
+ $dispatcher->subscribe(TestEvent1::class, $callback1);
+
+ $dispatcher->subscribe(
+ TestEvent2::class,
+ function () use (&$value) {
+ $value = false;
+ }
+ );
+
+ $dispatcher->dispatch(new TestEvent1());
+
+ $this->assertTrue($value);
+
+ //
+
+ $dispatcher->unsubscribe(TestEvent1::class, $callback1);
+
+ $value = false;
+
+ $dispatcher->dispatch(new TestEvent1());
+
+ $this->assertFalse($value);
+ }
+
+ private function getOriginProvider(): OriginProvider
+ {
+ $originProvider = $this->createMock(OriginProvider::class);
+
+ $originProvider->method('get')
+ ->willReturn('test-id');
+
+ return $originProvider;
+ }
+}
diff --git a/tests/unit/Espo/Core/Utils/Event/TestCiEvent1.php b/tests/unit/Espo/Core/Utils/Event/TestCiEvent1.php
new file mode 100644
index 0000000000..9e89933c60
--- /dev/null
+++ b/tests/unit/Espo/Core/Utils/Event/TestCiEvent1.php
@@ -0,0 +1,52 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Utils\Event;
+
+use Espo\Core\Utils\Event\CrossInstanceEvent;
+use stdClass;
+
+class TestCiEvent1 implements CrossInstanceEvent
+{
+ public function __construct(
+ public string $value,
+ ) {}
+
+ public static function fromRaw(stdClass $payload): static
+ {
+ return new self($payload->value ?? '');
+ }
+
+ public function toRaw(): stdClass
+ {
+ return (object) [
+ 'value' => $this->value,
+ ];
+ }
+}
diff --git a/tests/unit/Espo/Core/Utils/Event/TestCiEvent2.php b/tests/unit/Espo/Core/Utils/Event/TestCiEvent2.php
new file mode 100644
index 0000000000..083164e09c
--- /dev/null
+++ b/tests/unit/Espo/Core/Utils/Event/TestCiEvent2.php
@@ -0,0 +1,46 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Utils\Event;
+
+use Espo\Core\Utils\Event\CrossInstanceEvent;
+use stdClass;
+
+class TestCiEvent2 implements CrossInstanceEvent
+{
+ public static function fromRaw(stdClass $payload): static
+ {
+ return new self();
+ }
+
+ public function toRaw(): stdClass
+ {
+ return (object) [];
+ }
+}
diff --git a/tests/unit/Espo/Core/Utils/Event/TestEvent1.php b/tests/unit/Espo/Core/Utils/Event/TestEvent1.php
new file mode 100644
index 0000000000..01b8f21473
--- /dev/null
+++ b/tests/unit/Espo/Core/Utils/Event/TestEvent1.php
@@ -0,0 +1,35 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Utils\Event;
+
+use Espo\Core\Utils\Event\Event;
+
+class TestEvent1 implements Event
+{}
diff --git a/tests/unit/Espo/Core/Utils/Event/TestEvent2.php b/tests/unit/Espo/Core/Utils/Event/TestEvent2.php
new file mode 100644
index 0000000000..469a065f71
--- /dev/null
+++ b/tests/unit/Espo/Core/Utils/Event/TestEvent2.php
@@ -0,0 +1,35 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Utils\Event;
+
+use Espo\Core\Utils\Event\Event;
+
+class TestEvent2 implements Event
+{}
diff --git a/tests/unit/Espo/Core/Utils/Event/TestTransport.php b/tests/unit/Espo/Core/Utils/Event/TestTransport.php
new file mode 100644
index 0000000000..be217ad9d5
--- /dev/null
+++ b/tests/unit/Espo/Core/Utils/Event/TestTransport.php
@@ -0,0 +1,74 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Utils\Event;
+
+use Closure;
+use Espo\Core\Utils\Event\Envelope;
+use Espo\Core\Utils\Event\EventDispatcherTransport;
+use stdClass;
+
+class TestTransport implements EventDispatcherTransport
+{
+ /**
+ * @var (Closure(Envelope): void)|null
+ */
+ private ?Closure $callback = null;
+
+ /**
+ * @param Closure(Envelope): void $callback
+ */
+ public function subscribe(Closure $callback): void
+ {
+ $this->callback = $callback;
+ }
+
+ public function dispatch(Envelope $envelope): void
+ {}
+
+ public function shouldReconnect(): bool
+ {
+ return true;
+ }
+
+ public function dispatchForTest(string $eventClassName, stdClass $payload): void
+ {
+ if (!$this->callback) {
+ return;
+ }
+
+ $envelope = new Envelope(
+ eventClassName: $eventClassName,
+ payload: $payload,
+ origin: 'other',
+ );
+
+ ($this->callback)($envelope);
+ }
+}