RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

  • Rule Chain

  • Standard Components

    • Standard Components Overview
    • filter

      • jsFilter
        • Configuration
        • Relation Type
        • Execution Result
        • Configuration Examples
          • Basic Filtering Example
          • Filtering Example Based on Data Type
          • JSON Data Filtering Example
          • Binary Data Parsing and Filtering Example
          • Text Data Filtering Example
          • Composite Condition Filtering Example
        • Application Example
      • fieldFilter
      • msgTypeSwitch
      • jsSwitch
      • groupFilter
      • exprFilter
      • fork
      • Switch
    • action

    • transform

    • external

    • flow

  • Extension Components

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • RuleGo-Server

  • FAQ

  • Endpoint Module

  • Support

目录

jsFilter

jsFilter Component: JavaScript Script Filter. This component uses JavaScript scripts to filter messages (msg), metadata (metadata), and message types (msgType). The routing direction of the message is determined by the return value of the script (True or False chain).

JavaScript scripts support ECMAScript 5.1(+) syntax specifications and some ES6 features, such as async/await/Promise/let. Custom Go functions can be called within the script. Please refer to udf.

# Configuration

Field Type Description Default Value
jsScript string JavaScript script None
  • jsScript: The JavaScript script used to write filtering logic. The content of this field will be encapsulated within the following function body:

        function Filter(msg, metadata, msgType, dataType) { 
            ${jsScript} 
         }
    
    1
    2
    3

    Parameter Description:

    • msg: Message content
      • When dataType=JSON, the type is jsonObject, and properties can be accessed directly using dot notation, such as msg.temperature.
      • When dataType=BINARY, the type is Uint8Array, and the byte array can be manipulated directly, such as accessing the first byte with msg[0].
      • For other dataTypes, the type is string.
    • metadata: Message metadata, of type jsonObject.
    • msgType: Message type, of type string.
    • dataType: Message data type (JSON, TEXT, BINARY, etc.), which needs to be converted to a string using String(dataType).
    • Return value: Must return a boolean type to determine the routing direction of the message.

Notes

  1. The jsScript script must have a clear return value (return true/false).
  2. Script execution has a timeout limit, which can be configured through config.ScriptMaxExecutionTime.

# Relation Type

  • True: When the script returns true, the message will continue to be passed along the True chain path.
  • False: When the script returns false, the message will continue to be passed along the False chain path.
  • Failure: When the script execution fails, the message will continue to be passed along the Failure chain path.

# Execution Result

This component is a pure filtering component and will not modify the content of the incoming msg, metadata, and msgType.

# Configuration Examples

# Basic Filtering Example

  {
    "id": "s1",
    "type": "jsFilter",
    "name": "Filter",
    "configuration": {
      "jsScript": "return msg.temperature > 50;"
    }
  }
1
2
3
4
5
6
7
8

# Filtering Example Based on Data Type

  {
    "id": "s2",
    "type": "jsFilter",
    "name": "Binary Data Filtering",
    "configuration": {
      "jsScript": "if (String(dataType) === 'BINARY') { return msg.length > 10 && msg[0] === 0xFF; } else { return true; }"
    }
  }
1
2
3
4
5
6
7
8

# JSON Data Filtering Example

  {
    "id": "s3",
    "type": "jsFilter",
    "name": "JSON Data Filtering",
    "configuration": {
      "jsScript": "if (String(dataType) === 'JSON') { return msg.temperature > 25 && msg.humidity < 80; } else { return false; }"
    }
  }
1
2
3
4
5
6
7
8

# Binary Data Parsing and Filtering Example

  {
    "id": "s4",
    "type": "jsFilter",
    "name": "Device Protocol Filtering",
    "configuration": {
      "jsScript": "if (String(dataType) === 'BINARY' && msg.length >= 4) { var deviceId = (msg[0] << 8) | msg[1]; var functionCode = (msg[2] << 8) | msg[3]; return deviceId === 0x1001 && (functionCode === 0x0001 || functionCode === 0x0002); } return false;"
    }
  }
1
2
3
4
5
6
7
8

# Text Data Filtering Example

  {
    "id": "s5",
    "type": "jsFilter",
    "name": "Text Content Filtering",
    "configuration": {
      "jsScript": "if (String(dataType) === 'TEXT') { return msg.indexOf('ERROR') !== -1 || msg.indexOf('ALARM') !== -1; } return true;"
    }
  }
1
2
3
4
5
6
7
8

# Composite Condition Filtering Example

  {
    "id": "s6",
    "type": "jsFilter",
    "name": "Composite Condition Filtering",
    "configuration": {
      "jsScript": "var dt = String(dataType); if (dt === 'JSON') { return msg.level === 'critical'; } else if (dt === 'BINARY') { return msg.length > 0 && msg[0] === 0xAA; } else if (dt === 'TEXT') { return msg.includes('URGENT'); } return false;"
    }
  }
1
2
3
4
5
6
7
8

# Application Example

If the msgType is EVENT_APP1, push the message to: http://192.168.136.26:9099/app1/api/msg (opens new window) ; otherwise, push it to: http://192.168.136.26:9099/app2/api/msg (opens new window)

{
  "ruleChain": {
    "id":"rule01",
    "name": "Test Rule Chain",
    "root": true
  },
  "metadata": {
    "nodes": [
      {
        "id": "s1",
        "type": "jsFilter",
        "name": "Filter",
        "configuration": {
          "jsScript": "return msgType =='EVENT_APP1';"
        }
      },
      {
        "id": "s2",
        "type": "restApiCall",
        "name": "Push Data - app1",
        "configuration": {
          "restEndpointUrlPattern": "http://192.168.136.26:9099/app1/api/msg",
          "requestMethod": "POST",
          "maxParallelRequestsCount": 200
        }
      },
      {
        "id": "s3",
        "type": "restApiCall",
        "name": "Push Data - app2",
        "configuration": {
          "restEndpointUrlPattern": "http://192.168.136.26:9099/app2/api/msg",
          "requestMethod": "POST",
          "maxParallelRequestsCount": 200
        }
      }
    ],
    "connections": [
      {
        "fromId": "s1",
        "toId": "s2",
        "type": "True"
      },
      {
        "fromId": "s1",
        "toId": "s3",
        "type": "False"
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Edit this page on GitHub (opens new window)
Last Updated: 2025/06/23, 10:45:49
Standard Components Overview
fieldFilter

← Standard Components Overview fieldFilter→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式