NexusMessengerSerializer
Symfony Messenger SerializerInterface backed by a Nexus MessageSerializer; encodes and decodes envelopes for broker transports with full round-trip support for Nexus bridge stamps.
What it does
NexusMessengerSerializer implements Messenger's SerializerInterface so the bridge can use any Nexus MessageSerializer (Valinor, PHP-native, or custom) as the wire format for broker transports instead of Symfony's default PHP serializer.
Message bodies are serialized by the injected MessageSerializer. The message type travels in the type header. Encode and decode are asymmetric: encode uses the #[MessageType]-registered name when available and falls back to the FQCN; decode requires the header value to be registered in the TypeRegistry and throws MessageDecodingFailedException otherwise. To deliberately accept a FQCN header on decode, register the class as its own type name: $registry->register(Foo::class, Foo::class).
Bridge stamps round-trip as plain string headers and are fully restored on decode(). Non-bridge stamps are not preserved in v1 — if you need full Symfony stamp fidelity or interoperability with non-Nexus producers, swap in any other SerializerInterface.
Constructor
use Monadial\Nexus\Messenger\Serialization\NexusMessengerSerializer;
use Monadial\Nexus\Serialization\MessageSerializer;
use Monadial\Nexus\Serialization\TypeRegistry;
new NexusMessengerSerializer(
messages: MessageSerializer $messages,
types: TypeRegistry $types,
);
| Parameter | Type | Description |
|---|---|---|
$messages | MessageSerializer | Nexus serializer for message bodies. |
$types | TypeRegistry | Registry mapping #[MessageType] names to PHP class names and back. |
Methods
| Method | Signature | Description |
|---|---|---|
encode | encode(Envelope $envelope): array{body: string, headers: array<string, string>} | Serialize the message body and collect bridge stamp headers. |
decode | decode(array $encodedEnvelope): Envelope | Deserialize body and restore bridge stamps from headers. Throws MessageDecodingFailedException on a missing or unknown type header, or when the body key is absent or not a string. |
Wire headers
| Header | Value | Notes |
|---|---|---|
type | #[MessageType] name (encode: FQCN fallback) | Required. Encode falls back to FQCN when unregistered; decode throws MessageDecodingFailedException if the value is not in the TypeRegistry. |
X-Nexus-Correlation-Id | Correlation ID string | Present when a CorrelationIdStamp is on the envelope. |
X-Nexus-Reply-To | Reply-channel name | Present when a ReplyToStamp is on the envelope. |
X-Nexus-Source-Path | Actor path string | Present when a SourceActorPathStamp is on the envelope. |
X-Nexus-Target-Path | Actor path string | Present when a TargetActorPathStamp is on the envelope. |
X-Nexus-Producer-Identity | Producer identity string | Present when a ProducerIdentityStamp is on the envelope. Read by MapTargetAuthorizer to authorize producer → target routing (SEC-012). See the trust-boundary note under MessageRouter. |
X-Nexus-Trace-Context | JSON object {"traceparent":"…", …} | Present when a TraceContextStamp is on the envelope. Malformed JSON or non-string-map values are silently skipped on decode. |
Example
use Monadial\Nexus\Messenger\Serialization\NexusMessengerSerializer;
use Monadial\Nexus\Serialization\MessageSerializer;
use Monadial\Nexus\Serialization\TypeRegistry;
// Register the serializer with a Messenger transport
$serializer = new NexusMessengerSerializer($messageSerializer, $typeRegistry);
// Pass to transport factory — exact API depends on the transport implementation
$transport = new RedisTransport($connection, $serializer);
Only the bridge's own stamps — CorrelationIdStamp, ReplyToStamp, SourceActorPathStamp, TargetActorPathStamp, ProducerIdentityStamp, and TraceContextStamp — round-trip through the wire headers. All other Symfony stamps are dropped on encode and not reconstructed on decode. This is intentional in v1 — use a Symfony Serializer-backed serializer if you need full stamp fidelity.
Full API reference
Full class and method signatures
See also
- nexus-messenger package — bridge overview and full wiring guide
- nexus-serialization package —
MessageSerializerandTypeRegistry - Attributes — #[MessageType] — type name registered in
TypeRegistry - Messenger bridge guide — serializer wiring section