Load Balancer
TsgcWebSocketLoadBalancerServer — pluggable load balancer for sgcWebSockets back-ends with sticky sessions and health checks.
TsgcWebSocketLoadBalancerServer — pluggable load balancer for sgcWebSockets back-ends with sticky sessions and health checks.
The component TsgcWebSocketLoadBalancerServer allows you to load-balance WebSocket and HTTP protocols. For the WebSocket protocol, it distributes messages across a group of servers and distributes client connections using a random sequence or fewest-connections algorithm.
TsgcWebSocketLoadBalancerServer| Component class | TsgcWebSocketLoadBalancerServer (unit sgcWebSocket_LoadBalancer) |
| Frameworks | VCL, FireMonkey, Lazarus / FPC |
| Platforms | Windows, macOS, Linux, iOS, Android |
The principal published / public properties used to configure and drive the component. Consult the online help for the full list.
Active | Starts or stops the load balancer, opening the listening sockets that accept downstream clients and backend TsgcWebSocketServer registrations. |
Port | TCP port on which the load balancer accepts incoming WebSocket/HTTP clients and backend server registrations. |
HTTP2Options | Enables and tunes HTTP/2 on the load balancer's TLS listener used to serve HTTPS requests from downstream clients. |
SSLOptions | Holds certificate paths, TLS version selection and OpenSSL tuning for the load balancer's TLS listener. |
SecurityOptions | Defines admission rules such as allowed origins for WebSocket handshakes that reach the load balancer. |
Options | Miscellaneous behaviour flags for the load balancer: fragment handling, timeouts, HTTP test pages and UTF-8 validation. |
ThreadPoolOptions | Configures the size and upper bound of the reusable thread pool used when ThreadPool is enabled. |
Throttle | Caps the bandwidth (bits per second) that the load balancer reads from or writes to each connection. |
Bindings | Collection of IP/Port pairs the load balancer listens on for downstream clients and backend registrations. |
MaxConnections | Maximum number of concurrent TCP connections (downstream clients plus registered backends) accepted by the load balancer. |
The principal public methods exposed by the component.
Start() | Starts the load balancer from a secondary thread so the calling thread is not blocked while bindings are opened. |
Stop() | Stops the load balancer from a secondary thread so the calling thread is not blocked while connections are closed. |
ReStart() | Stops and then restarts the load balancer from a secondary thread, useful after changing bindings or ports at runtime. |
DisconnectAll() | Disconnects every active client connection and every registered backup server while keeping the load balancer listening for new connections. |
WriteData() | Sends a WebSocket message to a single client identified by its connection GUID, routing the frame through the backup server that owns the session. |
Ping() | Sends a WebSocket ping frame to every client connected through the load balancer. |
Broadcast() | Fans a WebSocket message out across every backup server in the cluster, optionally filtered by channel, protocol, or connection GUID list. |
PushPromiseAddPreLoadLinks() | Registers an HTTP/2 Server Push rule that preloads a set of related resources whenever a matching request path is served by the load balancer. |
PushPromiseRemovePreLoadLinks() | Removes the HTTP/2 Server Push rule previously registered on the load balancer for the given request path. |
The component exposes the following published events; consult the online help for full event-handler signatures.
OnBeforeSendServerBinding | TsgcWebSocketLoadBalancerServer › Events › OnBeforeSendServerBinding |
OnBinary | Fires when the load balancer itself receives a binary WebSocket frame on one of its downstream sessions. |
OnClientBinary | TsgcWebSocketLoadBalancerServer › Events › OnClientBinary |
OnClientConnect | TsgcWebSocketLoadBalancerServer › Events › OnClientConnect |
OnClientDisconnect | TsgcWebSocketLoadBalancerServer › Events › OnClientDisconnect |
OnClientFragmented | TsgcWebSocketLoadBalancerServer › Events › OnClientFragmented |
OnClientMessage | TsgcWebSocketLoadBalancerServer › Events › OnClientMessage |
OnConnect | Fires when a WebSocket connection (client or backend server) is established with the load balancer. |
OnDisconnect | Fires when any WebSocket connection accepted by the load balancer is closed. |
OnError | Fires when the load balancer detects an error on one of its accepted connections. |
OnException | Fires when an unhandled Delphi exception is caught by the load balancer while processing a connection. |
OnFragmented | Fires when the load balancer receives a fragmented WebSocket frame on one of its own sessions. |
OnHandshake | Fires after the load balancer validates an incoming WebSocket handshake and before the HTTP response is returned. |
OnLoadBalancerHTTPRequest | TsgcWebSocketLoadBalancerServer › Events › OnLoadBalancerHTTPRequest |
OnLoadBalancerHTTPResponse | TsgcWebSocketLoadBalancerServer › Events › OnLoadBalancerHTTPResponse |
OnMessage | Fires when the load balancer receives a text WebSocket frame on one of its own sessions. |
OnRawMessage | Fires when any WebSocket text frame arrives, before higher-level protocols or the forwarder process it. |
OnSSLAfterCreateHandler | TsgcWebSocketLoadBalancerServer › Events › OnSSLAfterCreateHandler |
OnSSLGetHandler | TsgcWebSocketLoadBalancerServer › Events › OnSSLGetHandler |
OnServerConnect | TsgcWebSocketLoadBalancerServer › Events › OnServerConnect |
OnServerDisconnect | TsgcWebSocketLoadBalancerServer › Events › OnServerDisconnect |
OnServerReady | Fires when a backend server has finished registering with the load balancer and is ready to accept traffic. |
Drop the component on a form, configure the properties below and activate it. The snippet that follows shows the typical OnBeforeSendServerBinding configuration sourced from the online help.
procedure OnBeforeSendServerBinding(Connection: TsgcWSConnection; var Binding: TsgcWSLoadBalancerServerBinding); begin // force secure WebSocket scheme when the client connected over TLS if Connection.IsSSL then Binding.Protocol := 'wss'; end;
void OnBeforeSendServerBinding(TsgcWSConnection *Connection, TsgcWSLoadBalancerServerBinding *&Binding) { if (Connection->IsSSL) Binding->Protocol = "wss"; }
void OnBeforeSendServerBinding(TsgcWSConnection Connection, out TsgcWSLoadBalancerServerBinding Binding) { if (Connection.IsSSL) Binding.Protocol = "wss"; }
The following scenarios are lifted verbatim from the online help. Each shows the configuration and method calls needed to drive the component through a specific real-world flow.
Fans a WebSocket message out across every backup server in the cluster, optionally filtered by channel, protocol, or connection GUID list.
oServer.Broadcast('Hello From Server');
oServer->Broadcast("Hello From Server");
oServer.Broadcast("Hello From Server");
Disconnects every active client connection and every registered backup server while keeping the load balancer listening for new connections.
oServer.DisconnectAll;
oServer->DisconnectAll();
oServer.DisconnectAll();
Fires when the load balancer itself receives a binary WebSocket frame on one of its downstream sessions.
procedure OnBinary(Connection: TsgcWSConnection; const Data: TMemoryStream); begin Log(Format('Received %d bytes from %s', [Data.Size, Connection.Guid])); end;
void OnBinary(TsgcWSConnection *Connection, const TMemoryStream *Data) { Log("Received " + IntToStr(Data->Size) + " bytes from " + Connection->Guid); }
void OnBinary(TsgcWSConnection Connection, TMemoryStream Data) { Console.WriteLine("Received " + Data.Size + " bytes from " + Connection.Guid); }
Fires when a binary frame is received from a downstream client before it is forwarded to the selected backend server.
procedure OnClientBinary(Connection: TsgcWSConnection; Data: TMemoryStream; var Handled: Boolean); begin // drop empty frames instead of forwarding them Handled := Data.Size = 0; end;
void OnClientBinary(TsgcWSConnection *Connection, TMemoryStream *Data, bool &Handled) { Handled = (Data->Size == 0); }
void OnClientBinary(TsgcWSConnection Connection, TMemoryStream Data, out bool Handled) { Handled = (Data.Size == 0); }
Fires when a downstream client finishes the WebSocket handshake against the load balancer and is paired with a backend server.
procedure OnClientConnect(ServerConnection: TsgcWSConnection; ClientConnection: TsgcWSLoadBalancerClientConnection); begin Log(Format('Client %s routed to backend %s', [ClientConnection.Guid, ServerConnection.Guid])); end;
void OnClientConnect(TsgcWSConnection *ServerConnection, TsgcWSLoadBalancerClientConnection *ClientConnection) { Log("Client " + ClientConnection->Guid + " routed to backend " + ServerConnection->Guid); }
void OnClientConnect(TsgcWSConnection ServerConnection, TsgcWSLoadBalancerClientConnection ClientConnection) { Console.WriteLine("Client " + ClientConnection.Guid + " routed to backend " + ServerConnection.Guid); }
Fires when a downstream client session is dropped from the load balancer, either because the client left or because the backend terminated.
procedure OnClientDisconnect(ServerConnection: TsgcWSConnection; ClientConnection: TsgcWSLoadBalancerClientConnection); begin Log('Client disconnected: ' + ClientConnection.Guid); end;
void OnClientDisconnect(TsgcWSConnection *ServerConnection, TsgcWSLoadBalancerClientConnection *ClientConnection) { Log("Client disconnected: " + ClientConnection->Guid); }
void OnClientDisconnect(TsgcWSConnection ServerConnection, TsgcWSLoadBalancerClientConnection ClientConnection) { Console.WriteLine("Client disconnected: " + ClientConnection.Guid); }
Every external claim links back to a primary source. The online-help references decode the canonical deep-link the company maintains for this component.
Demos\04.WebSocket_Other_Samples\03.LoadBalancer_Server