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
      • fieldFilter
      • msgTypeSwitch
      • jsSwitch
        • Configuration
        • Relation Type
        • Execution result
        • Configuration example
        • Application example
      • 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

目录

jsSwitch

jsSwitch component: script routing. Executes the configured JS script. The script should return an array of the names of the next or multiple chains to which the message should be routed.

JavaScript script supports ECMAScript 5.1(+) syntax specification and some ES6 specifications, such as: async/await/Promise/let. It allows calling Go custom functions in the script, please refer to udf .

# Configuration

Field Type Description Default value
jsScript string js script None
  • jsScript: You can filter msg, metadata, msgType. This field is the following function body content

        function JsSwitch(msg, metadata, msgType) { 
            ${jsScript} 
         }
    
    1
    2
    3
    • msg: Message content, if dataType=JSON, type is: jsonObject, you can use msg.temperature to operate. If dataType is other types, the field type is:string
    • metadata: Message metadata, type: jsonObject
    • msgType: Message type
    • Function return value type: array, an array of the names of the next or multiple chains to which the message should be routed

Note

Script execution timeout configuration reference: config.ScriptMaxExecutionTime

# Relation Type

Use the script custom return value to connect to the next or multiple nodes, if there is no matching node, the Default matching node will be used.

# Execution result

This component will not change the content of msg, metadata and msgType.

# Configuration example

  {
    "id": "s1",
    "type": "jsSwitch",
    "name": "Script routing",
    "configuration": {
      "jsScript": "return ['one','two'];"
    }
  }
1
2
3
4
5
6
7
8

# Application example

Using this component, you can dynamically control the logic of connecting to the next or multiple nodes. For example: Automatic marketing system, after receiving an event, trigger a query of the visitor data for the last 3 days and 30 days (these two nodes are triggered in parallel), and then process the data separately.

{
  "ruleChain": {
    "id":"rule01",
    "name": "Test rule chain",
    "root": true
  },
  "metadata": {
    "nodes": [
      {
        "id": "s1",
        "type": "jsSwitch",
        "name": "Script routing",
        "configuration": {
          "jsScript": "if msgType=='aa'{ return ['visitors_3days','visitors_30days'];}else {return ['visitors_3days','visitors_30days','visitors_60days'];}"
        }
      },
      {
        "id": "s2",
        "type": "dbClient",
        "name": "Query visitors for the last 3 days",
        "configuration": {
          "dbType":"mysql",
          "dsn":"root:root@tcp(127.0.0.1:3306)/test",
          "sql":"select * from users where DATE_SUB(CURDATE(), INTERVAL 3 DAY)<=visitor_time"
        }
      },
      {
        "id": "s3",
        "type": "dbClient",
        "name": "Query visitors for the last 30 days",
        "configuration": {
          "dbType":"mysql",
          "dsn":"root:root@tcp(127.0.0.1:3306)/test",
          "sql":"select * from users where DATE_SUB(CURDATE(), INTERVAL 30 DAY)<=visitor_time"
        }
      },
      {
        "id": "s4",
        "type": "dbClient",
        "name": "Query visitors for the last 60 days",
        "configuration": {
          "dbType":"mysql",
          "dsn":"root:root@tcp(127.0.0.1:3306)/test",
          "sql":"select * from users where DATE_SUB(CURDATE(), INTERVAL 60 DAY)<=visitor_time"
        }
      }
    ],
    "connections": [
      {
        "fromId": "s1",
        "toId": "s2",
        "type": "visitors_3days"
      },
      {
        "fromId": "s1",
        "toId": "s3",
        "type": "visitors_30days"
      },
      {
        "fromId": "s1",
        "toId": "s4",
        "type": "visitors_60days"
      }
    ]
  }
}
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Edit this page on GitHub (opens new window)
Last Updated: 2025/04/02, 01:29:50
msgTypeSwitch
groupFilter

← msgTypeSwitch groupFilter→

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

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