Real-Time Communication in Delphi — Choose Your Protocol

WebSocket, Server-Sent Events, HTTP/2 server push, MQTT, AMQP and WebRTC all solve “push the client when something happens”. They have radically different operational profiles — this page is the cheat sheet for picking the right one.

One library, six real-time protocols

sgcWebSockets ships every mainstream Delphi real-time client and server in the box — so the question stops being “can I do this in Pascal?” and becomes “which protocol fits the job?”

Real-time communication in Delphi” is an umbrella term — in practice it covers six fundamentally different network primitives, each with different latency, ordering, fan-out, NAT-traversal and infrastructure properties. Choosing wrongly costs months: you ship a polling REST loop, hit scalability ceilings, retrofit a WebSocket layer, then discover MQTT would have been a better fit because your traffic is fundamentally pub/sub. This page is the short, opinionated guide.

Which protocol — in one table

Scan the rows; the column with the most green wins.

Need WebSocket SSE HTTP/2 push MQTT AMQP WebRTC
Bi-directionalYesNo (server→client only)NoYes (pub/sub)Yes (pub/sub)Yes (peer)
Fan-out broadcastServer-implementedPer-client onlyPer-stream onlyNative (topics)Native (exchanges)No (1:1)
Browser supportedYesYesYesVia WebSocketVia WebSocketYes
Through corporate proxyVia wss://443Yes (looks like HTTP)YesVia WSSVia WSSOften (TURN)
Guaranteed deliveryApp-levelApp-levelApp-levelQoS 1/2Ack’dSCTP reliable
Low-bandwidth devicesMediumLowMediumExcellentMediumMedium
P2P / NAT traversalNo (server)NoNoNo (broker)No (broker)Yes (ICE)
End-to-end encryptedTLS hop-by-hopTLS hop-by-hopTLS hop-by-hopTLS hop-by-hopTLS hop-by-hopDTLS-SRTP E2E
Server can run in DelphiYesYesYesUse external brokerUse external brokerYes (signalling + STUN/TURN)

Short version

WebSocket

Default choice. Bi-directional, low overhead, works in every browser. Pick it for live dashboards, chat, collaborative editing, custom application protocols. Component →

Server-Sent Events

Pick when traffic is server → client only and you want the simplest possible model: a long-lived HTTP response that streams text. Built into every browser without a library. SSE →

HTTP/2 push

Use to pre-emptively send sub-resources (CSS, JS, JSON) alongside the main document — cuts a round-trip. Not a general-purpose “push” channel. HTTP/2 →

MQTT

Pub/sub for IoT, telemetry, mobile presence, connected vehicles. QoS 1/2, retained messages, last-will, tiny wire overhead. Needs a broker. MQTT →

AMQP

Enterprise messaging: durable queues, exchanges, routing keys, dead-letter handling, RabbitMQ / Azure Service Bus / IBM MQ. Pick over MQTT when you need rich routing. AMQP →

WebRTC

Direct peer-to-peer with NAT traversal — data channels for collaboration, eventually audio/video. The only option for E2E-encrypted P2P. WebRTC →

What each one looks like

The minimum viable Delphi snippet to connect with each protocol — same library, same component pattern.

WebSocket

WS := TsgcWebSocketClient.Create(nil);
WS.URL := 'wss://echo.websocket.events';
WS.OnMessage := DoMessage;
WS.Active := True;
WS.WriteData('hello');

Server-Sent Events

SSE := TsgcWSPClient_SSE.Create(nil);
SSE.URL := 'https://stream.example.com/events';
SSE.OnEvent := DoEvent;
SSE.Active := True;

MQTT

MQTT := TsgcWSPClient_MQTT.Create(nil);
MQTT.Client := WSClient;
MQTT.OnMQTTPublish := DoMqttPublish;
WSClient.Active := True;
MQTT.Subscribe('sensors/+/temperature', mtqsAtLeastOnce);

AMQP

AMQP := TsgcWSPClient_AMQP.Create(nil);
AMQP.Client := WSClient;
AMQP.OnAMQPMessage := DoAmqpMessage;
WSClient.Active := True;
AMQP.Consume('orders');

HTTP/2 multiplexed GET

H2 := TsgcHTTP2Client.Create(nil);
H2.Host := 'api.example.com';
H2.Port := 443;
H2.TLS  := True;
H2.OnResponse := DoResponse;
H2.Connect;
for i := 1 to 10 do H2.Get(Format('/items/%d', [i]));

WebRTC data channel

RTC := TsgcWSPClient_WebRTC.Create(nil);
RTC.Client := WSSignal;
RTC.IceServers.Add.URL := 'stun:stun.l.google.com:19302';
RTC.OnDataChannelMessage := DoP2PMessage;
WSSignal.Active := True;
RTC.CreateDataChannel('chat', True, True);
RTC.CreateOffer;

Three mistakes worth avoiding

Polling REST in a loop

If you find yourself calling Get every two seconds and diffing the response, the answer is almost always SSE (server→client only) or WebSocket (bi-directional). The CPU and bandwidth savings are routinely 95 %+.

Picking WebSocket when MQTT fits

If your traffic is fundamentally topic-based fan-out (devices publishing telemetry, dashboards subscribing), running a custom JSON-over-WebSocket protocol re-implements MQTT badly. Use the broker.

Confusing HTTP/2 push with server push

HTTP/2 push promise is a sub-resource optimisation, not a general push channel. For long-lived server-to-client streams you still want SSE, WebSocket or MQTT.

Explore each protocol

Delphi WebSocket component

The flagship transport.

Delphi MQTT client

IoT and telemetry pub/sub.

Delphi HTTP/2 client

Multiplexed HTTP and push.

Delphi WebRTC library

Peer-to-peer data channels.

Server-Sent Events

Simple server→client streaming.

AMQP

Enterprise messaging via RabbitMQ, Service Bus, IBM MQ.

Blog: SSE client walkthrough

Practical SSE example.

Blog: Load-balancing real-time

How to put nginx / Envoy / ALB in front of WebSocket and HTTP/2.

Try every protocol from one installer

Download the trial — demos for WebSocket, SSE, MQTT, AMQP, HTTP/2 and WebRTC all ship in the box.