Allows operators to specify a maximum size for incoming WebSocket messages.
Separate limits can be applied to clients and upstreams.
When an incoming message exceeds the limit:
- A close frame with status code
1009
is sent to the sender
- A close frame with status code
1001
is sent to the peer
- Both sides of the connection are closed
Configuration Reference
This plugin is compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway
declaratively.
Therefore, the Admin API is mostly read-only. The only tasks it can perform are all
related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Example plugin configuration
Enable on a service
Enable on a route
Enable globally
The following examples provide some typical configurations for enabling
the websocket-size-limit
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=websocket-size-limit" \
--data "config.client_max_payload=1024" \
--data "config.upstream_max_payload=16384"
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: websocket-size-limit-example
config:
client_max_payload: 1024
upstream_max_payload: 16384
plugin: websocket-size-limit
Next, apply the KongPlugin resource to a
service by annotating the
service as follows:
apiVersion: v1
kind: Service
metadata:
name: SERVICE_NAME|SERVICE_ID
labels:
app: SERVICE_NAME|SERVICE_ID
annotations:
konghq.com/plugins: websocket-size-limit-example
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: SERVICE_NAME|SERVICE_ID
selector:
app: SERVICE_NAME|SERVICE_ID
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: websocket-size-limit
service: SERVICE_NAME|SERVICE_ID
config:
client_max_payload: 1024
upstream_max_payload: 16384
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- In the Plugins section, click Add Plugin.
- Find and select the WebSocket Size Limit plugin.
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Services section, click View for the
service row.
- From the plugin section, click Add Plugin.
- Find and select the WebSocket Size Limit plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already pre-filled.
- Click Create.
The following examples provide some typical configurations for enabling
the websocket-size-limit
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=websocket-size-limit" \
--data "config.client_max_payload=1024" \
--data "config.upstream_max_payload=16384"
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: websocket-size-limit-example
config:
client_max_payload: 1024
upstream_max_payload: 16384
plugin: websocket-size-limit
Then, apply it to an ingress (route or routes)
by annotating the ingress as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ROUTE_NAME|ROUTE_ID
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: websocket-size-limit-example
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
service:
name: echo
port:
number: 80
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: websocket-size-limit
route: ROUTE_NAME
config:
client_max_payload: 1024
upstream_max_payload: 16384
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration
will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- Select a route.
- In the Plugins section, click Add Plugin.
- Find and select the WebSocket Size Limit plugin.
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Routes from the menu, then click View for the
route row.
- From the plugin section, click Add Plugin.
- Find and select the WebSocket Size Limit plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the route ID if it is not already prefilled.
- Click Create.
A plugin which is not associated to any service, route, or consumer is
considered global, and will be run on every request. Read the
Plugin Reference and the Plugin Precedence
sections for more information.
The following examples provide some typical configurations for enabling
the websocket-size-limit
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=websocket-size-limit" \
--data "config.client_max_payload=1024" \
--data "config.upstream_max_payload=16384"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-websocket-size-limit>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
client_max_payload: 1024
upstream_max_payload: 16384
plugin: websocket-size-limit
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: websocket-size-limit
config:
client_max_payload: 1024
upstream_max_payload: 16384
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Plugins from the menu, then click New Plugin.
- Find and select the WebSocket Size Limit plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, set the plugin scope to Global.
- Click Create.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string |
The name of the plugin, in this case websocket-size-limit . |
service.name or service.id
Type: string |
The name or ID of the service the plugin targets.
Set one of these parameters if adding the plugin to a service through the top-level /plugins endpoint.
Not required if using /services/SERVICE_NAME|SERVICE_ID/plugins . |
route.name or route.id
Type: string |
The name or ID of the route the plugin targets.
Set one of these parameters if adding the plugin to a route through the top-level /plugins endpoint.
Not required if using /routes/ROUTE_NAME|ROUTE_ID/plugins . |
enabled
Type: boolean
Default value: true |
Whether this plugin will be applied. |
config.client_max_payload
semi-optional
Type: integer
|
Maximum size (in bytes) of client-originated WebSocket messages. Must
be greater than 0 and less than 33554432 (32 MiB)
|
config.upstream_max_payload
semi-optional
Type: integer
|
Maximum size (in bytes) of upstream-originated WebSocket messages. Must
be greater than 0 and less than 33554432 (32 MiB)
|
Usage
Limits can be applied to client messages, upstream messages, or both.
Limit client messages to 4 KiB
With a database
Without a database
Use a request like this:
curl -i -X POST http://HOSTNAME:8001/services/SERVICE/plugins \
--data "name=websocket-size-limit" \
--data "config.client_max_payload=4096"
Add the following entry to the plugins:
section in the declarative configuration file:
plugins:
- name: websocket-size-limit
service: SERVICE
config:
client_max_payload: 4096
Limit upstream messages to 1 MiB
With a database
Without a database
Use a request like this:
curl -i -X POST http://HOSTNAME:8001/services/SERVICE/plugins \
--data "name=websocket-size-limit" \
--data "config.upstream_max_payload=1048576"
Add the following entry to the plugins:
section in the declarative configuration file:
plugins:
- name: websocket-size-limit
service: SERVICE
config:
upstream_max_payload: 1048576
Limit both client and upstream messages
With a database
Without a database
Use a request like this:
curl -i -X POST http://HOSTNAME:8001/services/SERVICE/plugins \
--data "name=websocket-size-limit" \
--data "config.client_max_payload=4096" \
--data "config.upstream_max_payload=1048576"
Add the following entry to the plugins:
section in the declarative configuration file:
plugins:
- name: websocket-size-limit
service: SERVICE
config:
client_max_payload: 4096
upstream_max_payload: 1048576
Raising the default limits
Kong Gateway applies the following default limits to incoming messages for all WebSocket
services:
Sender |
Default Limit |
client |
1048576 (1MiB ) |
upstream |
16777216 (16MiB ) |
This plugin can be used to increase the limit beyond the default. This example
increases the client limit to 2 MiB, up from the default of 1 MiB:
With a database
Without a database
Use a request like this:
curl -i -X POST http://HOSTNAME:8001/services/SERVICE/plugins \
--data "name=websocket-size-limit" \
--data "config.client_max_payload=2097152"
Add the following entry to the plugins:
section in the declarative configuration file:
plugins:
- name: websocket-size-limit
service: SERVICE
config:
client_max_payload: 2097152
The default client limit is smaller than the default upstream limit because proxying client-originated messages is much more computationally expensive than
upstream messages. This is due to the client-to-server masking required by the WebSocket
specification, so
in general it is wise to maintain a lower limit for client messages.
How the plugin works
How limits are applied
Note: Limits are evaluated based on the message payload length and not the
entire length of the WebSocket frame (header and payload).
Standalone data frames (text
and binary
)
For limits of 125 bytes or less, the message length is checked after reading
and decoding the entire message into memory.
For limits of 125 bytes or more, the message length is checked from the
frame header before the entire message is read from the socket buffer,
allowing Kong Gateway to close the connection without having to read, and potentially
unmask, the entire message into memory.
Continuation data frames
Kong Gateway aggregates continuation
frames, buffering them in-memory before forwarding
them to their final destination. In addition to evaluating limits on an
individual frame basis, like singular text
and binary
frames, Kong Gateway
also tracks the running size of all the frames that are buffered for
aggregation. If an incoming continuation
frame causes the total buffer size to
exceed the limit, the message is rejected, and the connection is closed.
For example, assuming client_max_payload = 1024
:
.------. .----.
|Client| |Kong|
'------' '----'
| |
| text(fin=false, len=500, msg=[...]) |
|>------------------------------------------->| # buffer += 500 (500)
| |
| |
| continue(fin=false, len=500, msg=[...]) |
|>------------------------------------------->| # buffer += 500 (1000)
| |
| |
| continue(fin=false, len=500, msg=[...]) |
|>------------------------------------------->| # buffer += 500 (1500)
| | # buffer >= 1024 (limit exceeded!)
| |
| close(status=1009, msg="Payload Too Large") |
|<-------------------------------------------<|
.------. .----.
|Client| |Kong|
'------' '----'
For control frames
All control frames (ping
, pong
, and close
) have a max payload size of
125
bytes, as per the WebSocket
specification. Kong Gateway
does not enforce any limits on control frames, even when they’re set to a value lower
than 125
.
See also