Ana içeriğe geç

WebSocket API Proxy Creation

Overview

bilgi

Apinizer WebSocket proxy works exactly the same logic as HTTP APIs. You can route both HTTP and WebSocket requests through the same API Proxy.

Basic Concepts

Backend URL

The only required configuration. Specifies the address of your backend WebSocket service.

Example: http://backend:9090/ws/api

Relative Path

Works the same logic as HTTP APIs. You can perform flexible routing using wildcards.

Examples: /websocket/*, /chat/*

Protocol Conversion

The system automatically performs protocol conversion:

  • http://ws://
  • https://wss://

How It Works?

Unified Path Resolve Process

Path Resolve Example

The following example shows how a WebSocket request is processed:

Incoming Request: ws://host/api/websocket/rooms/123?user=john
├─ API Proxy Path: /api/websocket/*
├─ Backend URL: http://localhost:9090/ws/chat
├─ Protocol Conversion: ws://localhost:9090/ws/chat
├─ Remaining Path: rooms/123
└─ Final URL: ws://localhost:9090/ws/chat/rooms/123?user=john
Path Resolve Steps

Processing of WebSocket requests goes through the following steps:

Query Separation

Query parameters are separated from the request path.

Path Normalization

Wildcard signs (*) are cleaned.

Remaining Path Calculation

API proxy path is extracted and the remaining path is determined.

Protocol Conversion

HTTP → WebSocket protocol conversion is performed (http://ws://, https://wss://).

URL Combination

Final URL is created by combining Backend URL + Remaining Path + Query parameters.

Practical Examples

HTTP and WebSocket Unified API Proxy

Routing both HTTP and WebSocket requests through the same API Proxy:

Configuration:

  • API Proxy: /api/v1/*
  • Backend URL: http://localhost:8080/service

Example Requests:

  • HTTP: POST /api/v1/usershttp://localhost:8080/service/users
  • WebSocket: ws://host/api/v1/chatws://localhost:8080/service/chat
Secure Connection (WSS)

Secure WebSocket connection with HTTPS backend:

Configuration:

  • API Proxy: /secure/*
  • Backend URL: https://api.example.com/ws

Example Request:

  • WebSocket: ws://host/secure/datawss://api.example.com/ws/data
ipucu

When HTTPS backend is used, it is automatically converted to WSS (WebSocket Secure) protocol.

WebSocket Chat Application

WebSocket proxy example for chat application:

Configuration:

  • API Proxy: /chat/*
  • Backend URL: http://chat-service:9090/websocket

Example Request:

  • WebSocket: ws://host/chat/room/123ws://chat-service:9090/websocket/room/123

Frequently Asked Questions

Can the same API Proxy support both HTTP and WebSocket?
ipucu

Yes! Both protocols work under the same relative path.

You can route both HTTP and WebSocket requests with the same API Proxy configuration. The system automatically detects the protocol type of the incoming request and processes it appropriately.

Which protocol should I use in Backend URL?
bilgi

Use HTTP or HTTPS. The system automatically converts to WS/WSS.

You can use http:// or https:// protocol in Backend URL. Apinizer automatically performs conversion for WebSocket requests:

  • http://ws://
  • https://wss://
Are query parameters preserved?
ipucu

Yes, all query parameters are transferred to the backend as-is.

Query parameters (?param=value) in WebSocket requests are preserved and transferred to the backend. For example:

ws://host/api/chat/room/123?user=john&token=abc

This request is routed to the backend with query parameters.

How can I make my existing HTTP APIs WebSocket-enabled?
Go to Environment Settings

Go to the settings of the environment that the API Proxy is connected to.

Enable WebSocket Support

Enable WebSocket support from environment settings.

Deploy

Deploy the changes. Your existing HTTP API Proxy will now also support WebSocket requests.

ipucu

No additional configuration is required for your existing HTTP API Proxies. You just need to enable WebSocket support from environment settings.