Websockets¶
WebSockets Standard by WHATWG organization
The WebSocket Protocol by Internet Engineering Task Force (IETF) organization
Ocelot supports proxying WebSockets with some extra bits [1].
Configuration¶
To enable WebSockets proxying with Ocelot, you need to do the following in your Program:
var app = builder.Build();
app.UseWebSockets();
await app.UseOcelot();
await app.RunAsync();
Then, in your ocelot.json, add the following to proxy a route using WebSockets:
{
"UpstreamPathTemplate": "/",
"DownstreamPathTemplate": "/ws",
"DownstreamScheme": "ws",
"DownstreamHostAndPorts": [
{ "Host": "localhost", "Port": 5001 }
]
}
With this configuration, Ocelot will match any WebSockets traffic that comes in on / and proxy it to localhost:5001/ws.
For clarity, Ocelot will receive messages from the upstream client, proxy them to the downstream service, receive messages from the downstream service, and then proxy them back to the upstream client.
Handy Links¶
WHATWG: WebSockets Standard
Mozilla Developer Network: The WebSocket API (WebSockets)
Microsoft Learn: WebSockets support in ASP.NET Core
Microsoft Learn: WebSockets support in .NET
SignalR [2]¶
Welcome to Real-time ASP.NET with SignalR
Ocelot supports proxying SignalR. To enable this with Ocelot, you need to do the following:
First, install the SignalR Client NuGet package:
Install-Package Microsoft.AspNetCore.SignalR.Client
Note: SignalR is part of the ASP.NET Core and can be referenced as follows:
<ItemGroup> <FrameworkReference Include="Microsoft.AspNetCore.App" /> </ItemGroup>More information on framework compatibility can be found in the instructions: Use ASP.NET Core APIs in a class library.
Second, you need to configure your application to use SignalR. A complete reference can be found here: ASP.NET Core SignalR configuration.
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddSignalR();
Note: Make sure to pay attention to the transport-level configuration for WebSockets. Ensure that allowed transports are properly configured to enable WebSockets connections: ASP.NET Core SignalR configuration.
Next, include the following in your ocelot.json file to proxy a route using SignalR.
Note that standard Ocelot routing rules apply; the key aspect is that the scheme is set to ws (WebSockets).
{
"UpstreamPathTemplate": "/gateway/{catchAll}",
"DownstreamPathTemplate": "/{catchAll}",
"DownstreamScheme": "ws",
"DownstreamHostAndPorts": [
{ "Host": "localhost", "Port": 5001 }
]
}
WebSocket Secure¶
If you define a route with the secured WebSockets protocol, use the wss scheme:
"DownstreamScheme": "wss",
Keep in mind that you can use WebSocket SSL for both SignalR 2 and Websockets.
Note: To understand
wssscheme, browse to this documentation:
IETF | The WebSocket Protocol: WebSocket URIs
Microsoft Learn: Secure your connection with TLS/SSL
Microsoft Learn: Search for “secure websocket”
If you want to ignore SSL warnings (errors), configure your route as follows: [3]
"DownstreamScheme": "wss",
"DangerousAcceptAnyServerCertificateValidator": true,
However, we strongly advise against this! Refer to the official notes regarding SSL Errors in the Configuration documentation. There, you can also explore best practices tailored for your environments.
Supported¶
This means you can configure your downstream services to run WebSockets and either:
Include multiple
DownstreamHostAndPortsin your route configuration.Connect your route to a Service Discovery provider. This allows you to load balance requests, which we think is pretty cool!
Not Supported¶
Unfortunately, many Ocelot features are not specific to WebSockets, such as header handling and HTTP client functionalities. Below is a list of features that will not work:
We cannot be entirely sure how this feature will behave once it is widely used. Therefore, thorough testing is strongly recommended!
Gotchas¶
WebSockets and SignalR are being actively developed by the .NET community. It is important to stay updated with trends and regularly check for new releases in the official documentation:
As a team, we are unable to provide direct development advice.
However, feel free to ask questions or explore coding recipes in Discussions of the repository.
Additionally, we welcome any bug reports, enhancement suggestions, or proposals related to this feature. ![]()
Note: The Ocelot team considers the current implementation of the WebSockets feature to be obsolete, as it is based on the WebSocketsProxyMiddleware class. WebSockets are a part of the ASP.NET Core framework, which includes the native WebSocketMiddleware class. We have a strong intention to either migrate or redesign this feature. For more details, see issue 1707.