Skip to content

Group Lines

Description

Use the testLine method to build routing groups and the getGroupResult method to find the most cost-effective (CP value) line within the group. These methods help you efficiently manage multiple server lines and ensure traffic is allocated to the best-performing server.

⚠️ Notes

  • When the return value is an empty string (""), be sure to implement a fallback mechanism to prevent errors or service disruptions.
  • Each line in the returned result (result) must be tested to ensure its availability. If a line is not usable, switch to other available lines or implement a fallback mechanism as needed.
  • The type and group parameters must match the group names previously configured through testLine, otherwise, performance data may not be retrieved correctly.

testLine( type, group, lines )

Description

The testLine method is used to create a new routing group based on the specified group name. This method helps in building and managing routing lines. It supports optional parameters, allowing flexible configuration of test type, group name, and server lines.


Syntax

javascript
import JBLWebSocket from '@/assets/js/jbl.es.js';
const authorizationKey = ${Product authorization key};

JBLWebSocket.connect(authorizationKey);
JBLWebSocket.testLine(type, group, lines);

Parameters

ParameterTypeDescription
typeStringRequired. specifies the type, options are site or 3r, corresponding to a site or third-party service.
groupStringRequired. the group name used to identify a routing group.
linesStringRequired. server line addresses, formatted as a comma-separated string (e.g., api.dome1.com,api.dome2.com).

Examples

Basic Example

The following example creates a routing group named "Group1" with two server lines:

javascript
JBLWebSocket.testLine('site', 'Group1', 'api.dome1.com,api.dome2.com');

Setting Type for Third-Party Services

This example creates a routing group related to third-party services:

javascript
JBLWebSocket.testLine('3r', 'ThirdPartyGroup', 'api.thirdparty.com,api.backup.com');

Use Cases

  1. Routing Configuration: Set up routing plans for specific group names to facilitate traffic distribution for websites or services.
  2. Multi-Server Testing: Configure multiple server lines to test connectivity or allocate traffic.
  3. Group Management: Easily manage server lines for different groups, improving configuration efficiency and maintainability.

getGroupResult( type, group )

Description

The getGroupResult method is used to retrieve the latest performance metrics (speed and usage) of server lines from different regions, based on the specified type and group name. This allows clients to dynamically select the best-performing route at any given time.


Syntax

javascript
import JBLWebSocket from '@/assets/js/jbl.es.js';
const authorizationKey = ${Product authorization key};

JBLWebSocket.connect(authorizationKey);
let jblResult = await JBLWebSocket.getGroupResult(type, group);

Parameters

ParameterTypeDescription
typeStringRequired. specifies the type, either site or 3r.
groupStringRequired. the name of the routing group.

Example Usage

Basic Example

The following example retrieves the best-performing line for the group "Group1" under the type site:

javascript
let jblResult = await JBLWebSocket.getGroupResult('site', 'Group1');
console.log(jblResult); 
// Example Output: { site_Group1: 'api.dome1.com' }

Result processing

When the return value is an empty string, you need to implement your own fallback mechanism to avoid potential errors. For example:

javascript
let jblResult = await JBLWebSocket.getGroupResult('site', 'Group1');

if (!jblResult.site_Group1) {
    console.error('No available lines. Please implement a fallback mechanism.');
    // Add your fallback logic here
} else {
    console.log(`Best line: ${jblResult.site_Group1}`);

    // Example of testing the line's availability
    const fetchTestLine = async (url) => {
        try {
            const response = await fetch(url, { method: 'HEAD' });
            if (response.ok) {
                console.log(`${url} is available.`);
                return true;
            } else {
                console.warn(`${url} is unavailable.`);
                return false;
            }
        } catch (err) {
            console.error(`${url} test failed:`, err);
            return false;
        }
    };

    const isAvailable = await fetchTestLine(jblResult.site_Group1);

    if (!isAvailable) {
        console.error('Primary line is not usable. Switching to a fallback line...');
        // Add your fallback logic here
    }
}

Use Cases

  1. Real-Time Routing: Enables clients to dynamically adjust routing based on the latest performance data.
  2. Multi-Region Optimization: Provides insights into server performance in different regions to ensure optimal user experience.
  3. Fallback Mechanism: Supports seamless handling of scenarios where no suitable lines are available.