WebSocket Handler
WebSocket handlers are an Enterprise-only feature at this time. Please contact us to trial this or sign up for an Enterprise account.
Zuplo provides two handlers for proxying WebSocket connections to your backend WebSocket APIs:
webSocketHandlerproxies WebSocket traffic straight through to your backend without inspecting the messages. Use this when you only need to authenticate, rate limit, or route the connection.webSocketPipelineHandlerdoes everythingwebSocketHandlerdoes and additionally runs every message through a policy pipeline, so you can inspect, transform, or drop individual messages in either direction. See the WebSocket Pipeline Handler.
Both handlers can be configured alongside other existing policies like Rate Limiting, API Keys, etc. and are available for use on all environments.
These handlers are only configurable via the JSON View on a project's Route
Designer or directly in your project's *.oas.json file.
Setup in routes.oas.json
This section covers the passthrough webSocketHandler. To intercept messages,
see the WebSocket Pipeline Handler.
Configuration of the WebSocket Handler is similar to other available handlers.
Set the name of the path that your WebSocket API route will use, set the use of
the webSocketHandler export from @zuplo/runtime module in the handler
configuration and use the rewritePattern property inside of options to point
to your service's WebSocket API endpoint.
Your configuration will look like below:
Code
Handler Options
The WebSocket Handler accepts the following options in the options property:
rewritePattern(required): The URL pattern for the backend WebSocket endpoint. Supports JavaScript string interpolation syntax for dynamic URL construction based on request data and environment variables.policies(optional,webSocketPipelineHandleronly): Configures the message-interception policies that run on each WebSocket frame. See the WebSocket Pipeline Handler.
Similar to other handlers using rewritePattern, it supports JavaScript string
interpolation syntax and can be used to shape the URL based on data from the
incoming request and environment variables defined in the project.
Code
The following objects are available for substitution:
env- the environment object, to access Environment Variablesrequest: ZuploRequest- the fullZuploRequestobjectcontext: ZuploContext- theZuploContextobject without functions.params: Record<string, string>- The parameters of the route. For example,params.productIdwould be the value of:productIdin a route.query: Record<string, string>- The query parameters of the route. For example,query.filterBywould be the value of?filterBy=VALUE.method: string- The HTTP method of the incoming request. For example,GETorPOST.headers: Headers- the incoming request's headers objecturl: string- The full incoming request as a stringhost: string- Thehostportion of the incoming URLhostname: string- Thehostnameportion of the incoming URLorigin: string- Theoriginportion of the incoming URLpathname: string- Thepathnameportion of the incoming URLport: string- Theportportion of the incoming URLsearch- Thesearchportion of the incoming URL
Use the following methods to encode portions of the URL:
encodeURIComponent: TheencodeURIComponent()function encodes a URI by replacing each instance of certain characters with escape sequences.e: An alias toencodeURIComponentto help keep URLs more readable. Can be used like${e(params.productId)}
Example Values
A few examples of the values of various substitutions.
${headers.get("content-type")}-"application/json"${host}-"example.com:8080"${hostname}-"example.com"${method}-"GET"${origin}-"https://example.com"${params.productId}-":productId"${pathname}-"/v1/products/:productId"${port}-"8080"${query.category}-"cars"${search}-"?category=cars"${url}-"https://example.com:8080/v1/products/:productId?category=cars"${env.BASE_URL}-"https://example.com"
Different Backends per Environment
It's common to want to use different backends for your production, staging and preview environments. This can be achieved by using environment variables to specify the origin of the backend.
For example,
Code
Using the rewritePattern in options you can combine the BASE_PATH
environment variable, say https://example.com to achieve this.
Code