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 Examples
          • Basic Routing Example
          • Multi-Routing Example
          • Routing Based on Data Type Example
          • JSON Data Routing Example
          • Binary Device Data Routing Example
          • Text Log Routing Example
          • Mixed Data Handling and Routing 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-based Router. This component executes the configured JS script and dynamically routes messages to one or more output chains based on the script's return value.

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. For more details, refer to udf.

# Configuration

Field Type Description Default Value
jsScript string JavaScript script None
  • jsScript: This field can process and evaluate msg, metadata, msgType, and dataType. It is the body of the following function:

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

    Parameter Description:

    • msg: Message content
      • When dataType=JSON, it is of type jsonObject, and fields can be accessed using msg.temperature.
      • When dataType=BINARY, it is of type Uint8Array, and the byte array can be manipulated directly, such as accessing the first byte with msg[0].
      • For other dataTypes, it is of type 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 type: Array, returning a string array containing one or more chain names to route to.

Note

  1. The script execution timeout configuration can be found at config.ScriptMaxExecutionTime.
  2. The returned chain names must be defined in the rule chain connections; otherwise, the message will be discarded.

# Relation Type

The routing relationship is dynamically determined by the script's return value. Messages can be routed to one or multiple output chains. If the returned chain name does not have a corresponding connection, the Default chain will be used for routing.

# Execution Result

This component does not modify the content of msg, metadata, and msgType. It is only used to determine the routing direction of the message.

# Configuration Examples

# Basic Routing Example

  {
    "id": "s1",
    "type": "jsSwitch",
    "name": "Script-based Routing",
    "configuration": {
      "jsScript": "if (msg.temperature > 50) return ['highTemp']; else if (msg.temperature < 10) return ['lowTemp']; else return ['normalTemp'];"
    }
  }
1
2
3
4
5
6
7
8

# Multi-Routing Example

  {
    "id": "s2",
    "type": "jsSwitch",
    "name": "Multi-Routing",
    "configuration": {
      "jsScript": "var routes = []; if (msgType === 'ALARM') routes.push('alarm'); if (msg.priority === 'high') routes.push('priority'); return routes;"
    }
  }
1
2
3
4
5
6
7
8

# Routing Based on Data Type Example

  {
    "id": "s3",
    "type": "jsSwitch",
    "name": "Data Type Routing",
    "configuration": {
      "jsScript": "var dt = String(dataType); if (dt === 'BINARY') { if (msg.length > 1024) return ['largeBinary']; else return ['smallBinary']; } else if (dt === 'JSON') return ['jsonData']; else return ['textData'];"
    }
  }
1
2
3
4
5
6
7
8

# JSON Data Routing Example

  {
    "id": "s4",
    "type": "jsSwitch",
    "name": "JSON Data Routing",
    "configuration": {
      "jsScript": "if (String(dataType) === 'JSON') { var routes = []; if (msg.temperature > 50) routes.push('highTemp'); if (msg.humidity > 80) routes.push('highHumidity'); if (msg.level === 'critical') routes.push('critical'); return routes.length > 0 ? routes : ['normal']; } return ['skip'];"
    }
  }
1
2
3
4
5
6
7
8

# Binary Device Data Routing Example

  {
    "id": "s5",
    "type": "jsSwitch",
    "name": "Device Data Routing",
    "configuration": {
      "jsScript": "if (String(dataType) === 'BINARY' && msg.length >= 4) { var deviceId = (msg[0] << 8) | msg[1]; var functionCode = (msg[2] << 8) | msg[3]; if (deviceId === 0x1001) { if (functionCode === 0x0001) return ['sensorData']; else if (functionCode === 0x0002) return ['statusData']; else if (functionCode === 0x0010) return ['commandData']; } return ['unknownDevice']; } return ['invalidData'];"
    }
  }
1
2
3
4
5
6
7
8

# Text Log Routing Example

  {
    "id": "s6",
    "type": "jsSwitch",
    "name": "Log Routing",
    "configuration": {
      "jsScript": "if (String(dataType) === 'TEXT') { var routes = []; if (msg.includes('ERROR')) routes.push('errorLog'); if (msg.includes('WARN')) routes.push('warnLog'); if (msg.includes('DEBUG')) routes.push('debugLog'); return routes.length > 0 ? routes : ['infoLog']; } return ['nonTextData'];"
    }
  }
1
2
3
4
5
6
7
8

# Mixed Data Handling and Routing Example

  {
    "id": "s7",
    "type": "jsSwitch",
    "name": "Mixed Data Routing",
    "configuration": {
      "jsScript": "var dt = String(dataType); var routes = []; if (msgType === 'ALARM') routes.push('alarm'); if (dt === 'JSON' && msg.priority === 'high') routes.push('priority'); else if (dt === 'BINARY' && msg.length > 0 && msg[0] === 0xFF) routes.push('protocolData'); else if (dt === 'TEXT' && msg.includes('URGENT')) routes.push('urgent'); return routes.length > 0 ? routes : ['default'];"
    }
  }
1
2
3
4
5
6
7
8
Edit this page on GitHub (opens new window)
Last Updated: 2025/06/23, 10:45:49
msgTypeSwitch
groupFilter

← msgTypeSwitch groupFilter→

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

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