The publisher automatically enables both local (iceoryx2) and network (Zenoh) transport. Local transport is always available, while network transport runs asynchronously on a background thread.
// Create with network disabled initiallylet publisher = Publisher::<SensorData>::create_with_session( "sensors/temperature", false, // Start with network disabled None // No shared session)?;// Enable network later (e.g., when remote subscriber discovered)// This is typically handled by TopicManager
let data = SensorData { temperature: 23.5, timestamp: 1234567890,};publisher.send(data)?;
The send() method returns immediately. For local subscribers, the message is available in < 1 μs. For network subscribers, serialization and network send happen asynchronously on a background thread.
Local path: Message is written directly to shared memory (synchronous, fast)
Network path: Message is queued to background thread (non-blocking)
Background thread: Serializes and sends over Zenoh (asynchronous)
The network path uses latest-message semantics. If the network is slow and multiple messages arrive, only the most recent message is sent. This ensures network subscribers always get the latest data.
let subscriber = Subscriber::<SensorData>::create( "sensors/temperature", Some(false) // Force local transport)?;
If you force local transport but no local publisher exists, receive() will always return None. Use this only when you’re certain a local publisher is running.
For continuous message reception, use a polling loop:
Copy
Ask AI
loop { if let Ok(Some(data)) = subscriber.receive() { // Process message process_sensor_data(data); } // Small sleep to avoid busy-waiting std::thread::sleep(std::time::Duration::from_millis(1));}
For high-frequency data, consider removing the sleep. The receive() call is very fast (< 1 μs for local transport), so busy-waiting may be acceptable for real-time applications.
Type validation occurs when messages are deserialized. Mismatched types result in deserialization errors:
Copy
Ask AI
# Type mismatch will cause deserialization errortry: data = WrongType.from_bytes(sample.payload)except ValueError as e: print(f"Type mismatch: {e}")
Type safety prevents common errors like sending the wrong message type or mismatched field layouts. This is especially important for cross-language communication.
// First publisher establishes typelet pub1 = Publisher::<SensorData>::create("topic")?;// This will fail if DifferentType != SensorDatalet pub2 = Publisher::<DifferentType>::create("topic")?;// Error: Type mismatch for topic 'topic'
Network transport unavailable:
Copy
Ask AI
// If network transport fails to initializelet publisher = Publisher::<SensorData>::create("topic")?;// Network errors are logged but don't affect local publishing
Network failures are logged but don’t propagate to the caller. This ensures local communication continues even if the network is down. Check logs if network communication isn’t working.
Unless you have a specific reason, use auto-detection:
Copy
Ask AI
// ✅ Good: Auto-detects best transportlet subscriber = Subscriber::<T>::create("topic", None)?;// ⚠️ Only if you need specific behaviorlet subscriber = Subscriber::<T>::create("topic", Some(true))?;