pub struct Envelope {
pub namespace: String,
pub msg_type: String,
pub payload: Value,
pub timestamp: Option<u64>,
pub from: Option<String>,
}Expand description
A namespace-routed message. All application messages use this format over WebSocket connections. truffle-core NEVER inspects the payload.
§Fields
namespace: Application-owned routing key (e.g.,"ft","chat").msg_type: Application-defined message type within the namespace.payload: Opaque JSON value. truffle-core never deserializes this.timestamp: Optional millisecond Unix timestamp, set by the sender.from: Sender peer ID, typically set by the receiving side (Layer 5).
§Forward compatibility
Unknown fields in the JSON representation are silently ignored during deserialization, allowing older nodes to receive messages from newer protocol versions without error.
Fields§
§namespace: StringNamespace owned by the application (e.g., "ft", "chat", "bus").
Used for routing to the correct subscriber.
msg_type: StringApplication-defined message type within the namespace.
payload: ValueOpaque payload. truffle-core never deserializes this further.
timestamp: Option<u64>Millisecond Unix timestamp, set by the sender.
from: Option<String>Sender peer ID, typically populated on the receiving side.
Implementations§
Source§impl Envelope
impl Envelope
Sourcepub fn new(namespace: &str, msg_type: &str, payload: Value) -> Self
pub fn new(namespace: &str, msg_type: &str, payload: Value) -> Self
Create a new envelope with the given namespace, message type, and payload.
The timestamp and from fields are left as None. Use
with_timestamp to stamp the current time.
Sourcepub fn with_timestamp(self) -> Self
pub fn with_timestamp(self) -> Self
Set the timestamp to the current Unix time in milliseconds.
Sourcepub fn serialize(&self) -> Result<Vec<u8>, EnvelopeError>
pub fn serialize(&self) -> Result<Vec<u8>, EnvelopeError>
Serialize this envelope to JSON bytes using the default codec.
Sourcepub fn deserialize(data: &[u8]) -> Result<Self, EnvelopeError>
pub fn deserialize(data: &[u8]) -> Result<Self, EnvelopeError>
Deserialize an envelope from JSON bytes using the default codec.