Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

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

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

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

Check out the full source code here (GitHub link)

Exit mobile version