RAP Logo

filter

Filter messages based on specified criteria to enable selective processing of data streams.

filter Block

The filter block is a Report by Exception (RBE) node - only passes on data if the payload has changed. It can also block unless, or ignore if the value changes by a specified amount (Dead- and Narrowband mode).

Overview

The filter block evaluates incoming messages against defined rules and decides whether to pass them through or block them. It supports various filtering modes:

  • Property Filtering: Filter based on message properties
  • Value Filtering: Filter based on specific values
  • Expression Filtering: Filter using JavaScript expressions
  • Type Filtering: Filter based on data types
  • Conditional Filtering: Filter based on complex conditions

Configuration Options

Filter Mode

Choose the type of filtering to apply:

Simple Property Filter

  • Property: The message property to check
  • Operator: Comparison operator (equals, not equals, contains, etc.)
  • Value: The value to compare against

Expression Filter

  • Expression: JavaScript expression to evaluate
  • Expected Result: What the expression should return to pass the filter

Type Filter

  • Property: The message property to check
  • Type: Expected data type (string, number, boolean, object, array)

Filter Behavior

  • Pass Through: What happens when the filter condition is met
  • Block: What happens when the filter condition is not met
  • Multiple Rules: How to handle multiple filter rules

Common Filter Types

Property Exists Filter

Check if a property exists in the message:

// Configuration
Property: msg.payload.userId;
Operator: exists;

// Result: Passes if userId exists, blocks if it doesn't

Value Comparison Filter

Compare property values:

// Configuration
Property: msg.payload.status;
Operator: equals;
Value: "active";

// Result: Passes if status equals "active"

Range Filter

Filter numeric values within a range:

// Configuration
Property: msg.payload.score;
Operator: between;
Value: 80, 100;

// Result: Passes if score is between 80 and 100

Array Contains Filter

Check if an array contains a value:

// Configuration
Property: msg.payload.tags;
Operator: contains;
Value: "urgent";

// Result: Passes if tags array contains "urgent"

Expression Filter

Use complex JavaScript expressions:

// Configuration
Expression: msg.payload.priority === "high" && msg.payload.assigned !== null
Expected Result: true

// Result: Passes if both conditions are true

Use Cases

Data Validation

Filter out invalid or incomplete data:

input → filter (valid data) → processing → output

filter (invalid data) → error handling

Priority Processing

Route high-priority messages differently:

input → filter (high priority) → fast processing

filter (normal priority) → standard processing

Content Filtering

Filter messages based on content:

input → filter (contains keywords) → analysis

filter (no keywords) → standard processing

Rate Limiting

Filter messages to implement rate limiting:

input → filter (rate limit) → processing

filter (rate exceeded) → queue or drop

Common Patterns

Multi-Stage Filtering

// Stage 1: Filter by type
Property: msg.payload.type
Operator: equals
Value: "document"

// Stage 2: Filter by status
Property: msg.payload.status
Operator: equals
Value: "ready"

// Stage 3: Filter by size
Property: msg.payload.size
Operator: less than
Value: 10485760 // 10MB

Conditional Processing

// High priority path
Property: msg.payload.priority
Operator: equals
Value: "high"

// Normal priority path
Property: msg.payload.priority
Operator: not equals
Value: "high"

Data Quality Control

// Valid data
Expression: msg.payload && msg.payload.id && msg.payload.timestamp
Expected Result: true

// Invalid data
Expression: !msg.payload || !msg.payload.id
Expected Result: true

Advanced Filtering

Complex Expressions

Use JavaScript expressions for sophisticated filtering:

// Filter based on multiple conditions
Expression: msg.payload.user.role === "admin" || msg.payload.urgent === true;

// Filter based on calculations
Expression: msg.payload.score / msg.payload.maxScore > 0.8;

// Filter based on date/time
Expression: new Date(msg.payload.timestamp) > new Date("2024-01-01");

Dynamic Filtering

Modify filter behavior based on runtime conditions:

// Use context for dynamic filtering
Expression: msg.payload.category === flow.get("activeCategory");

// Use environment variables
Expression: msg.payload.environment === env.get("CURRENT_ENV");

State-Based Filtering

Filter based on flow state:

// Filter based on processing state
Expression: flow.get("processing") === false;

// Filter based on user session
Expression: flow.get("userSession") === msg.payload.userId;

Tips

  • Use Clear Conditions: Write filter conditions that are easy to understand
  • Test Edge Cases: Verify filter behavior with various input data
  • Consider Performance: Complex expressions may impact performance
  • Document Logic: Clearly document complex filter conditions
  • Use Multiple Filters: Chain filters for complex logic instead of one complex filter
  • Handle Errors: Consider what happens when filter expressions fail

Common Issues

Undefined Properties

Handle cases where properties don't exist:

// BAD: This will fail if property doesn't exist
Property: msg.payload.user.name;
Operator: equals;
Value: "admin";

// GOOD: Check for existence first
Expression: msg.payload.user && msg.payload.user.name === "admin";

Type Mismatches

Be aware of data type differences:

// BAD: String vs number comparison
Property: msg.payload.id;
Operator: equals;
Value: 123;

// GOOD: Convert types if needed
Expression: parseInt(msg.payload.id) === 123;

Performance Issues

Avoid expensive operations in filter expressions:

// BAD: Expensive operation in filter
Expression: msg.payload.data.split(",").length > 10

// GOOD: Use simple property checks when possible
Property: msg.payload.itemCount
Operator: greater than
Value: 10
  • switch - For routing based on conditions
  • function - For complex filtering logic
  • change - For modifying filtered data
  • debug - For monitoring filter behavior