Using Tyk as a server-sent events (SSE) Proxy
Tyk Gateway supports SSE proxying over HTTP, and can sit in the middle between the client and the SSE server and support the server sending updates to the client.Setup
- Enable SSE support on the Gateway: Set
http_server_options.enable_websockets
totrue
in your Tyk Gateway config file. - To maintain an open connection between the API consumer and the Tyk Gateway, set
http_server_options.read_timeout
andhttp_server_options.write_timeout
to appropriately high values (in milliseconds). For example, you could try setting both to2000
, but this is for you to determine in your environment. - Set
http_server_options.flush_interval
to an appropriate value, e.g.1
, to force Tyk to stream the response to the client everyn
seconds.
Example using Tyk as an SSE proxy
For this we will need:- An SSE server. For this example we will use Golang HTML 5 SSE example
- An instance of the Tyk Gateway and optionally the Tyk Dashboard
- Ensure the Gateway configurations detailed in the Setup section are set.
- Run the SSE server as per the example instructions. By default this runs on port
8000
.
- Publish an API with the following configuration:
- Set an appropriate listen path, e.g.
"listen_path": "/sse"
- Strip the listen path, e.g.
"strip_listen_path": true,
- Set the target url as the SSE server, e.g. the example SSE server:
"target_url": "http://host.docker.internal:8000"
- Click Save, and wait for the Gateway to reload the API before testing it
- Set an appropriate listen path, e.g.
- To test the protected SSE service via the API in the Tyk Gateway run: