Overview
Packet Interceptors allow you to intercept, inspect, and modify MQTT packets as they flow through HiveMQ. Interceptors enable:- Packet Inspection - Examine packet contents for logging or monitoring
- Packet Modification - Change packet properties, payloads, or user properties
- Message Enrichment - Add metadata or transform message payloads
- Protocol Translation - Convert between different data formats
- Filtering and Validation - Block or modify invalid packets
Interceptor Types
HiveMQ supports interceptors for all MQTT packet types:Inbound Interceptors
- CONNECT - Client connection requests
- PUBLISH - Inbound messages from clients
- SUBSCRIBE - Subscription requests
- UNSUBSCRIBE - Unsubscription requests
- DISCONNECT - Client disconnection
- PINGREQ - Ping requests
- AUTH - Authentication packets (MQTT 5.0)
Outbound Interceptors
- CONNACK - Connection acknowledgments
- PUBLISH - Outbound messages to clients
- SUBACK - Subscribe acknowledgments
- UNSUBACK - Unsubscribe acknowledgments
- DISCONNECT - Server-initiated disconnects
- PINGRESP - Ping responses
- PUBACK, PUBREC, PUBREL, PUBCOMP - QoS acknowledgments
Interceptors.java:27 for interceptor service interface.
Implementing Interceptors
Step 1: Register Interceptor Provider
Step 2: Implement Publish Inbound Interceptor
src/main/java/com/hivemq/extensions/interceptor/publish/.
Publish Interceptors
Inbound Publish Interceptor
Intercept messages from clients before routing:Outbound Publish Interceptor
Intercept messages before delivery to clients:Connect Interceptors
Inbound Connect Interceptor
Intercept client connection requests:ConnectInboundInterceptorHandler.java for internal implementation.
Outbound Connack Interceptor
Intercept connection acknowledgments:ConnackOutboundInterceptorHandler.java for internal implementation.
Subscribe Interceptors
Inbound Subscribe Interceptor
Intercept subscription requests:src/main/java/com/hivemq/extensions/interceptor/subscribe/.
Disconnect Interceptors
Inbound Disconnect Interceptor
Intercept client-initiated disconnects:DisconnectInterceptorHandler.java for internal implementation.
Outbound Disconnect Interceptor
Intercept server-initiated disconnects:Message Enrichment Example
Add metadata to all published messages:Protocol Translation Example
Convert between JSON and Protocol Buffers:Async Interceptors
Perform async operations in interceptors:Preventing Packet Delivery
Block packets from being processed:Best Practices
Performance
- Minimize Processing - Keep interceptor logic fast and lightweight
- Use Async Mode - Don’t block packet processing with I/O
- Avoid Unnecessary Modifications - Only modify packets when needed
- Cache Lookups - Cache frequently accessed data
- Batch Operations - Process multiple packets together when possible
Security
- Validate Payloads - Check payload size and format
- Sanitize Topics - Validate topic structure and content
- Filter Sensitive Data - Remove credentials from logs and metrics
- Implement Rate Limiting - Prevent abuse via interceptors
Reliability
- Handle Errors Gracefully - Don’t crash on malformed packets
- Set Appropriate Timeouts - Don’t block indefinitely
- Log Important Events - Track interceptor actions for debugging
- Test Edge Cases - Test with malformed and edge-case packets
Troubleshooting
Interceptor Not Called
- Verify interceptor provider is registered in
extensionStart() - Check extension is loaded and started
- Review packet type (inbound vs outbound)
- Enable debug logging for interceptor system
Packet Modifications Not Applied
- Use
output.getPublishPacket()to get modifiable packet - Verify modifications occur before async timeout
- Check that packet hasn’t been prevented from delivery
- Review interceptor execution order (extension priority)
Performance Degradation
- Profile interceptor execution time
- Use async mode for I/O operations
- Reduce number of registered interceptors
- Optimize packet modification logic
Interceptors.java:35 and Interceptors.java:51 for interceptor provider registration.
Next Steps
Authentication
Authenticate clients before packet processing
Authorization
Control packet authorization
Client Initializers
Register client-specific interceptors
Extension SDK
Learn more about the Extension SDK