HTMX Engine Server

TsgcHTMX_Engine_Server — handles htmx partial-page swap requests over HTTP and broadcasts live out-of-band fragments to all connected browsers over WebSocket.

TsgcHTMX_Engine_Server

Bridges htmx's HTTP-driven partial updates with WebSocket server-push. Incoming htmx requests are dispatched to OnHTMXRequest; your handler returns only the fragment to swap in. Call BroadcastFragment at any time to push live out-of-band updates to every connected browser.

Component class

TsgcHTMX_Engine_Server

Role

htmx swaps + live server push

Languages

Delphi, C++ Builder, .NET

Handle htmx requests and push live fragments

Wire the engine to your WebSocket server, handle partial-swap requests in OnHTMXRequest, and call BroadcastFragment to push updates to every connected browser.

uses
  sgcHTMX_Engine_Server;

var
  oHTMX: TsgcHTMX_Engine_Server;
begin
  oHTMX := TsgcHTMX_Engine_Server.Create(nil);
  oHTMX.Server         := WSSServer;
  oHTMX.OnHTMXRequest  := HandleHTMXRequest;

  // later, from any thread, push a KPI update:
  oHTMX.BroadcastFragment(
    '<div id="kpi-revenue" hx-swap-oob="true">' +
    CurrToStr(FRevenue) + '</div>');
end;
TsgcHTMX_Engine_Server *oHTMX = new TsgcHTMX_Engine_Server(NULL);
oHTMX->Server        = WSSServer;
oHTMX->OnHTMXRequest = HandleHTMXRequest;

// push a live update:
oHTMX->BroadcastFragment(
  "<div id=\"kpi-revenue\" hx-swap-oob=\"true\">" +
  CurrToStr(FRevenue) + "</div>");
var htmx = new TsgcHTMX_Engine_Server();
htmx.Server        = wssServer;
htmx.OnHTMXRequest += HandleHTMXRequest;

// push a live update:
htmx.BroadcastFragment(
    "<div id=\"kpi-revenue\" hx-swap-oob=\"true\">" +
    revenue.ToString("N0") + "</div>");

Key properties & methods

The members you reach for most often.

Server

The TsgcWSHTTPServer this engine attaches to; handles both HTTP partial-swap requests and WebSocket push.

OnHTMXRequest

Event fired for every htmx-triggered HTTP request; your handler returns just the fragment HTML to swap in.

BroadcastFragment

Sends an out-of-band htmx payload to every currently connected WebSocket client simultaneously.

HandleRequest

Called by the host server for each request; identifies htmx headers and routes to OnHTMXRequest or falls through.

hx-swap-oob

htmx's out-of-band swap attribute: the fragment id must match the target element id on the page.

Thread safety

BroadcastFragment is thread-safe; call it from background timers, push threads or event handlers without locking.

Keep exploring

Online HelpFull API reference and usage guide for this component.
All sgcHTML ComponentsBrowse the full feature matrix of 60+ components.
Download Free TrialThe 30-day trial ships the 60.HTML demo projects.
PricingSingle, Team and Site licenses with full source code.

Ready to Get Started?

Download the free trial and start building web UIs in Delphi, C++ Builder and .NET.