Overview
Interceptor, as the name suggests, intercepts messages passing through a channel and performs preset operations on them without causing any disruption to the message flow. Developers can code these custom operations/logic for data processing, data manipulation, logging, monitoring, moderating, and much more. A developer can develop anything using interceptors within its limitations; hence, the use cases for the interceptors are almost limitless.
Example Use Case: Passive Content Moderation in Enterprise Messaging
Consider a large organization that requires a robust internal messaging application with passive content moderation capabilities. Leveraging ART's flexible WebSocket infrastructure, this can be efficiently achieved through the implementation of custom Interceptors.
Developers can design and deploy Interceptors specifically engineered with language moderation features. As messages traverse the communication channel, these Interceptors analyze the content in real-time. Based on the defined interceptor logic, messages identified as containing inappropriate language can be:
- Blocked: Preventing the message from reaching its intended recipient.
- Flagged: Marking the message for review by human moderators.
- Censored: Automatically redacting or replacing objectionable terms.
This strategic application of Interceptors ensures that all users adhere to established communication standards, fostering a professional and compliant messaging environment. This approach provides a scalable and extensible solution for maintaining content quality and adherence to organizational policies within enterprise messaging platforms.
How Interceptor works?
Interceptors act as middleware within a connection, allowing developers to inspect, validate, or transform messages before they reach the recipient. While they may introduce slight delays, they do not block the flow of communication; instead, they enhance it with additional processing.
A connection can have a single interceptor or multiple interceptors, depending on the user's need:
Figure 1: Message pipeline with single interceptor intergration
Figure 2: Message pipeline with multiple interceptors
When multiple interceptors are connected, they process messages sequentially in pipeline order. Each interceptor can be either synchronous or asynchronous
If a pipeline contains a mix of sync and async interceptors:
- The message waits at each synchronous interceptor until processing completes.
- Once an asynchronous interceptor is reached, the pipeline moves forward immediately, allowing the next interceptor (sync or async) to start.
- This means some interceptors run sequentially, while others run in parallel depending on their type.
Message Flow Through Interceptors
- Message Capture: The system checks and routes messages to relevant interceptors.
- Input Validation: Each interceptor validates or formats input according to its schema/template.
- Processing: Custom logic (validation, transformation, API calls) is executed.
- Execution: Sync interceptors block until a response; async ones process in the background.
- Response Handling: The interceptor returns a success (resolve) or failure (reject).
- Forwarding: The processed message continues to the next interceptor or recipient.
Types of Interceptors
Interceptors can be categorized based on their execution model and connection modes. Understanding their distinct behaviors is crucial for effective integration and optimal performance within the messaging infrastructure.
Execution model
- Synchronous Interceptor: Block the message pipeline until processing completes and a response is returned.
- Asychronous Interceptor: Run in the background without blocking the pipeline, allowing parallel processing.
Mode of Communication
- HTTP requests: Forward messages to external HTTP endpoints for processing using a request-response model.
- Real-time: Process messages instantly over persistent connections like WebSocket or SSE for low-latency communication.
Scaling Interceptors
As the number of connections and the volume of messages increase, a single interceptor instance may not be sufficient to handle the entire load. In such scenarios, maintaining multiple interceptor instances becomes necessary. To effectively balance the workload across these instances, Load Distributors are employed. Different approaches are utilized to handle load distribution for Synchronous and Asynchronous Interceptors, which are discussed in detail in the next section.
