Help Center/ API Gateway/ Best Practices/ Forwarding WebSocket Services Using Dedicated Gateway
Updated on 2024-09-20 GMT+08:00

Forwarding WebSocket Services Using Dedicated Gateway

Scenario

You can create WebSocket APIs using APIG same as creating an HTTP API. WebSocket is a protocol for full-duplex communication over a single TCP connection. It outweighs HTTP in real-time, two-way communication between the client and server, proving invaluable for applications such as instant messaging, online gaming, and real-time data update in the financial sector.

Restrictions

  • WebSocket APIs cannot be debugged on the APIG console.
  • The WebSocket API has a timeout, and if the idle time of a WebSocket connection without ping/pong exceeds the configured timeout, the connection expires.

Prerequisites

Prepare a WebSocket service backend for which ping/pong keepalive has been performed.

Procedure

  1. Log in to the APIG console.
  2. Select a gateway at the top of the navigation pane.
  3. In the navigation pane, choose API Management > APIs.
  4. Click Create API > Create API and configure the frontend.

    Table 1 Frontend configuration

    Parameter

    Description

    API Name

    Enter a name that conforms to specific rules to facilitate search. For example, API01.

    Group

    The default option is DEFAULT.

    URL

    Method: Request method of the API. Set this parameter to GET.

    Protocol: Request protocol of the API. Default: HTTPS, which corresponds to the WebSocket Secure (WSS).

    Subdomain Name: The system automatically allocates a subdomain name to each API group for internal testing. The subdomain name can be accessed 1,000 times a day.

    Path: Path for requesting the API. In this example, enter /hello.

    Gateway Response

    Select a response to be displayed if the gateway fails to process an API request. default is selected by default.

    Authentication Mode

    API authentication mode. Select None. (None: Not recommended for actual services. All users will be granted access to the API.)

  5. Click Next and configure the backend information.

    Table 2 Backend configuration

    Parameter

    Description

    Backend Type

    Select HTTP&HTTPS.

    Load Balance Channel

    Determine whether the backend service will be accessed using a load balance channel. For this example, select Skip.

    URL

    • Method: Request method of the API. Set this parameter to GET.
    • Protocol: Set this parameter to HTTP.
    • Backend Address: Address and port of the WebSocket backend service.
    • Path: Path of the backend service. In this example, enter /.

    Timeout (ms)

    Adjust the backend timeout to be longer than the ping/pong heartbeat interval. For example, if the ping/pong heartbeat interval is 20s, the timeout can be set to (20000 ms, 60000 ms].

  6. Click Finish.
  7. After the API is created, click Publish Latest Version on the APIs page to publish the API.
  8. Use the API test tool to call the API.

    In this practice, enter wss://IP address/hello to call an API in the DEFAULT group. The IP address is the EIP in the Gateway Information on the APIG console.