Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiIDERAD Studio

Working Example Code of Using STOMP In Your Application

smartmockups kp1ck3ys

STOMP is the Simple (or Streaming) Text Oriented Messaging Protocol. STOMP provides an interoperable wire format so that STOMP clients can communicate with any STOMP message broker to provide easy and widespread messaging interoperability among many languages, platforms, and brokers. You may use IDE Software such as Delphi for STOMP. You can check the latest version of the STOMP specification here. You can read the explanation about STOMP from the article by Embarcadero MVP Ian Barker which can be found here https://blogs.embarcadero.com/add-stomp-to-your-apps-for-lightweight-real-time-streaming/

I wrote this article to complement what Ian Barker wrote, with code samples and components from Embarcadero Technology Partner, ESEGECE, https://www.esegece.com/delphi/stomp

screen shot 2021 05 23 at 21 34 51

Keep in mind that STOMP is just a protocol, a protocol that forms the basic specification in text, so STOMP data transmission can use anything, including using WebSockets.

How to use the sgcWebSocket component as the STOMP carrier data?

Let’s download the WebSocket component, trial version here https://www.esegece.com/websockets/download

The sgcWebSocket components support most Delphi versions from Delphi 7 to Delphi 10.4 as well as Lazarus/ Freepascal.

Delphi 10_4  
Delphi 10_3Delphi 10_2Delphi 10_1
Delphi 10Delphi XE8Delphi XE7
Delphi XE6Delphi XE5Delphi XE4
Delphi XE3Delphi XE2Delphi XE
Delphi 2010Delphi 2009Delphi 2007
Delphi 7Lazarus 2.0.12 Win32
  • Run your Delphi
  • Open Package’s folder of your dowloaded component
  • Open sgcWebSocketsD10_x.groupproj and install it
screen shot 2021 05 23 at 22 03 47
  • Now, create a new Firemonkey cross-platform project and add code like below:

Adding “frmMain” code

object frmMain: TfrmMain
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu]
  BorderStyle = Single
  Caption = #144'Delphi STOMP Client'
  ClientHeight = 495
  ClientWidth = 384
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  DesignerMasterStyle = 0
  object btnSubscribe: TButton
    Position.X = 32.000000000000000000
    Position.Y = 32.000000000000000000
    TabOrder = 0
    Text = 'Subscribe'
    OnClick = btnSubscribeClick
  end
  object edtTopic: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    TabOrder = 1
    Position.X = 120.000000000000000000
    Position.Y = 32.000000000000000000
    Size.Width = 233.000000000000000000
    Size.Height = 22.000000000000000000
    Size.PlatformDefault = False
  end
  object Label1: TLabel
    Position.X = 120.000000000000000000
    Position.Y = 16.000000000000000000
    Text = 'Topic'
    TabOrder = 2
  end
  object btnPublish: TButton
    Position.X = 32.000000000000000000
    Position.Y = 80.000000000000000000
    TabOrder = 3
    Text = 'Publish'
    OnClick = btnPublishClick
  end
  object edtMessage: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    TabOrder = 4
    Position.X = 120.000000000000000000
    Position.Y = 80.000000000000000000
    Size.Width = 233.000000000000000000
    Size.Height = 22.000000000000000000
    Size.PlatformDefault = False
  end
  object Label2: TLabel
    Position.X = 120.000000000000000000
    Position.Y = 64.000000000000000000
    Text = 'Message'
    TabOrder = 5
  end
  object Memo1: TMemo
    Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
    DataDetectorTypes = []
    Position.X = 32.000000000000000000
    Position.Y = 120.000000000000000000
    Size.Width = 321.000000000000000000
    Size.Height = 345.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 6
    Viewport.Width = 317.000000000000000000
    Viewport.Height = 341.000000000000000000
  end
end

Lets create Method “InitSTOMPClient”

procedure TfrmMain.InitSTOMPClient;
begin
  FClient := TsgcWebSocketClient.Create(nil);
  FClient.Host := 'www.esegece.com';
  FClient.Port := 15674;
  FClient.Options.Parameters := '/ws';

  FStomp := TsgcWSPClient_STOMP.Create(nil);
  FStomp.Client := FClient;
  FStomp.Authentication.Enabled := True;
  FStomp.Authentication.UserName:= 'sgc';
  FStomp.Authentication.Password:= 'sgc';
  FStomp.OnSTOMPConnected := DoOnSTOMPConnected;
  FStomp.OnSTOMPMessage   := DoOnSTOMPMessage;

  FClient.Active := True;
end;

FClient is WebSocket client for STOMP to get passed to STOMP server, ESEGECE have a server as a test, let’s just use that

Create two event handlers – “DoOnSTOMPConnected” and “DoOnSTOMPMessage”

procedure TfrmMain.DoOnSTOMPConnected(Connection: TsgcWSConnection; Version,
  Server, Session, HeartBeat, RawHeaders: string);
begin
  Caption := Caption + ' of ' + FClient.Host;
end;

procedure TfrmMain.DoOnSTOMPMessage(Connection: TsgcWSConnection; MessageText,
  Destination, MessageId, Subscription, ACK, ContentType, RawHeaders: string);
begin
  Memo1.Lines.Add('MessageText: '+MessageText)
end;

The application display will be as follows

screen shot 2021 05 23 at 22 28 03

Check out the full source code here (GitHub link)

RAD Studio 13.1 Florence Now Available See What's New in RAD Studio 13.1 Delphi is 31 - Webinar Replay

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.

Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Senior Software Engineer | Delphi/FreePascal Enthusiast | Linux/ Unix Enthusiast

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES