trigger
Advanced triggering and timing control for workflow execution with sophisticated trigger logic.
trigger Block
The trigger block when triggered, can send a message, and then optionally a second message, unless extended or reset.
Overview
The trigger block is more sophisticated than the basic inject block and provides several triggering modes:
- Send and Wait: Send a message and wait for a response
- Send and Timeout: Send a message with a timeout period
- Interval Triggering: Send messages at regular intervals
- Conditional Triggering: Trigger based on specific conditions
- State Management: Maintain state between trigger events
Configuration Options
Trigger Mode
Choose the type of triggering behavior:
Send and Wait
- Send Message: The message to send initially
- Wait For: What to wait for (response, timeout, or condition)
- Timeout: Maximum time to wait (in milliseconds)
Interval Triggering
- Interval: Time between trigger events (in milliseconds)
- Repeat: Number of times to repeat (0 = infinite)
- Initial Delay: Delay before first trigger
Conditional Triggering
- Condition: JavaScript expression to evaluate
- Check Interval: How often to check the condition
Message Configuration
- Payload: The data to send with the trigger
- Topic: Optional topic for the message
- Properties: Additional message properties
Timeout Settings
- Timeout Duration: Maximum wait time
- Timeout Action: What to do when timeout occurs
- Retry Logic: Whether to retry on timeout
Use Cases
API Request with Timeout
Implement API calls with timeout handling:
trigger → http request → response
↓
timeout → error handlingPeriodic Data Collection
Collect data at regular intervals:
trigger (interval) → data collection → processingConditional Processing
Trigger based on specific conditions:
trigger (condition) → conditional processingState Machine Control
Control state transitions:
trigger → state change → next triggerCommon Patterns
Request-Response with Timeout
// Configuration
Mode: Send and Wait
Timeout: 5000ms
Payload: { requestId: "123", data: "test" }
// Flow
trigger → http request → http response → trigger (response)
↓
timeout → error handlingPeriodic Monitoring
// Configuration
Mode: Interval
Interval: 30000ms (30 seconds)
Repeat: 0 (infinite)
// Flow
trigger → check status → log results → wait for next triggerConditional Execution
// Configuration
Mode: Conditional
Condition: flow.get("ready") === true
Check Interval: 1000ms
// Flow
trigger → process data → set ready = false → wait for conditionAdvanced Features
State Management
The trigger block can maintain state between executions:
// Access previous state
var lastTrigger = flow.get("lastTrigger") || 0;
var now = Date.now();
// Update state
flow.set("lastTrigger", now);Dynamic Configuration
Modify trigger behavior based on runtime conditions:
// Adjust interval based on conditions
var interval = msg.payload.priority === "high" ? 1000 : 5000;
node.interval = interval;Error Handling
Implement robust error handling:
// Handle timeout
if (msg.timeout) {
node.warn("Trigger timeout occurred");
// Implement fallback logic
}Tips
- Set Appropriate Timeouts: Choose timeout values that match your use case
- Handle Edge Cases: Consider what happens when conditions are never met
- Use State Wisely: Leverage flow context for state management
- Monitor Performance: Be mindful of frequent condition checking
- Test Thoroughly: Verify trigger behavior under various conditions
- Document Logic: Clearly document complex trigger conditions
Common Issues
Infinite Loops
Avoid creating infinite loops with conditional triggers:
// BAD: This will create an infinite loop
Condition: true;
// GOOD: Use meaningful conditions
Condition: flow.get("processing") === false;Memory Leaks
Clean up resources when using interval triggers:
// Clean up on node stop
node.on("close", function () {
if (node.interval) {
clearInterval(node.interval);
}
});Race Conditions
Handle concurrent trigger events properly:
// Use locks to prevent race conditions
if (flow.get("processing")) {
return; // Skip if already processing
}
flow.set("processing", true);