STOMP Client Component: sgcMQ | eSeGeCe

STOMP Client

TsgcWSPClient_STOMP is a generic STOMP 1.0, 1.1 and 1.2 client. STOMP is a text frame protocol, so it is easy to trace and works against almost any broker that offers a messaging endpoint. This component covers the standard frame set, with broker-specific descendants available for RabbitMQ and ActiveMQ.

TsgcWSPClient_STOMP

Advertise the protocol versions you accept through Versions and the broker picks one during the CONNECT exchange. The negotiated version arrives in OnSTOMPConnected.

Component class

TsgcWSPClient_STOMP

Specification

STOMP 1.0, 1.1 and 1.2

Transport

TCP (61613) or TLS

Languages

Delphi, C++ Builder

Transport: plain TCP and TLS. sgcMQ connects over plain TCP and TLS. If you need STOMP over WebSocket, that requires a sgcWebSockets package, which provides the WebSocket client.

Subscribe with an id, then send

STOMP identifies subscriptions by an id you choose, which is the same id you pass to UnSubscribe and the one that comes back on every inbound MESSAGE frame.

uses
  sgcTCP_Client_WS, sgcWebSocket_Classes, sgcWebSocket_Protocols,
  sgcWebSocket_Protocol_STOMP_Client;

var
  TCPClient: TsgcTCPClient;
  STOMP: TsgcWSPClient_STOMP;
begin
  TCPClient := TsgcTCPClient.Create(nil);
  TCPClient.Host := 'broker.example.com';
  TCPClient.Port := 61613;

  STOMP := TsgcWSPClient_STOMP.Create(nil);
  STOMP.Client := TCPClient;
  STOMP.Versions.V1_2 := True;
  STOMP.Options.VirtualHost := '/';
  STOMP.Authentication.Enabled  := True;
  STOMP.Authentication.UserName := 'guest';
  STOMP.Authentication.Password := 'guest';
  STOMP.HeartBeat.Enabled := True;

  STOMP.OnSTOMPConnected := STOMPConnected;
  STOMP.OnSTOMPMessage   := STOMPMessage;

  TCPClient.Active := True;
end;

procedure TForm1.STOMPConnected(Connection: TsgcWSConnection;
  Version, Server, Session, HeartBeat, RawHeaders: string);
begin
  STOMP.Subscribe('sub-0', '/queue/orders');
  STOMP.Send('/queue/orders', '{"orderId":12345}');
end;

procedure TForm1.STOMPMessage(Connection: TsgcWSConnection;
  MessageText, Destination, MessageId, Subscription, ACK, ContentType,
  RawHeaders: string);
begin
  Memo1.Lines.Add(Destination + ': ' + MessageText);
  STOMP.ACK(MessageId);
end;
// include: sgcTCP_Client_WS.hpp, sgcWebSocket_Protocols.hpp,
// sgcWebSocket_Protocol_STOMP_Client.hpp

TsgcTCPClient *TCPClient = new TsgcTCPClient(this);
TCPClient->Host = "broker.example.com";
TCPClient->Port = 61613;

TsgcWSPClient_STOMP *STOMP = new TsgcWSPClient_STOMP(this);
STOMP->Client = TCPClient;
STOMP->Versions->V1_2 = true;
STOMP->Authentication->Enabled  = true;
STOMP->Authentication->UserName = "guest";
STOMP->Authentication->Password = "guest";

STOMP->OnSTOMPConnected = STOMPConnected;
STOMP->OnSTOMPMessage   = STOMPMessage;

TCPClient->Active = true;

// In the OnSTOMPConnected handler:
STOMP->Subscribe("sub-0", "/queue/orders");
STOMP->Send("/queue/orders", "{\"orderId\":12345}");

Key properties & methods

The members you reach for most often.

Sending

Send(destination, text, contentType, transaction, customHeaders) writes a SEND frame. The last three arguments are optional, so a simple send is two parameters.

Subscribing

Subscribe(id, destination, ack, customHeaders) registers a subscription under an id of your choosing, and UnSubscribe(id) removes it. The ack mode defaults to ackAuto.

Acknowledging

ACK(id, transaction) confirms a message and NACK(id, transaction) refuses it. Both take the message id that arrived with the MESSAGE frame.

Transactions

BeginTransaction, CommitTransaction and AbortTransaction take a transaction name. Pass that name to Send and ACK to enrol them in the transaction.

Message event

OnSTOMPMessage hands you the body, destination, message id, subscription id, ack mode, content type and the raw header block, so nothing from the frame is lost.

Version negotiation

Versions.V1_0, V1_1 and V1_2 control what the client advertises. The version the broker picked is the first argument of OnSTOMPConnected.

Receipts and errors

OnSTOMPReceipt fires when the broker confirms a frame, OnSTOMPError when it rejects one, both with the receipt id and raw headers.

Heart-beating

HeartBeat configures the outgoing and incoming intervals negotiated on CONNECT. Ping sends one manually and OnSTOMPPing reports the exchange.

Connection options

Options.VirtualHost becomes the host header on CONNECT, and Options.DisconnectTimeout bounds how long a graceful Disconnect waits for the broker's receipt.

Keep exploring

Online HelpFull API reference and usage guide.
STOMP for RabbitMQThe descendant with RabbitMQ destination helpers.
STOMP for ActiveMQThe descendant tuned for Apache ActiveMQ.
Download Free TrialConnect to any STOMP-capable broker.
PricingSingle, Team and Site licenses with full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and send your first STOMP frame from Delphi or C++ Builder.