From sgcWebSockets 2024.2.0 AMQP 1.0.0 is supported.
AMQP (Advanced Message Queuing Protocol) 1.0.0 is a messaging protocol designed for reliable, asynchronous communication between distributed systems. It facilitates the exchange of messages between applications or components in a decoupled manner, allowing them to communicate without direct dependencies.
Overall, AMQP 1.0.0 provides a standardized and interoperable way for different software components and systems to communicate in a loosely coupled manner, making it suitable for various distributed and enterprise-level applications.
The AMQP 1.0.0 client has the property AMQPOptions where you can configure the connection.
The AMQP Authentication must be configured in the Authentication property.
The connection starts with the client (usually a messaging application or service) initiating a TCP connection to the server (the message broker). The client connects to the server's port, typically 5672 for non-TLS connections and 5671 for TLS-secured connections. Once the TCP connection is established, the client and server negotiate the AMQP protocol version they will use. AMQP 1.0.0 supports various versions, and during negotiation, both parties agree on using version 1.0.0.
After protocol negotiation, the client may need to authenticate itself to the server, depending on the server's configuration. Authentication mechanisms can include SASL (Simple Authentication and Security Layer) mechanisms like PLAIN, EXTERNAL, or others supported by the server.
Example: connect to AMQP server listening on secure port 5671 and using SASL credentials
// Creating AMQP client oAMQP := TsgcWSPClient_AMQP1.Create(nil); // Setting AMQP authentication options oAMQP.AMQPOptions.Authentication.AuthType := amqp1authSASLPlain; oAMQP.AMQPOptions.Authentication.Username := 'sgc'; oAMQP.AMQPOptions.Authentication.Password := 'sgc'; // Creating WebSocket client oClient := TsgcWebSocketClient.Create(nil); // Setting WebSocket specifications oClient.Specifications.RFC6455 := False; // Setting WebSocket client properties oClient.Host := 'www.esegece.com'; oClient.Port := 5671; oClient.TLS := True; // Assigning WebSocket client to AMQP client oAMQP.Client := oClient; // Activating WebSocket client oClient.Active := True;
Once authenticated, the client opens an AMQP session. A session is a logical context for communication between the client and server. Sessions are used to group related messaging operations together. Use the method CreateSession to create a new session, the method allows to set the session name or leave empty and the component will assign automatically one.
If the session has been created successfully, the event OnAMQPSessionOpen will be fired with the details of the session.
AMQP1.CreateSession('MySession'); procedure AMQP1AMQPSessionOpen(Sender: TObject; const aSession: TsgcAMQP1Session; const aBegin: TsgcAMQP1FrameBegin); begin ShowMessage('#session-open: ' + aSession.Id); end;
Within a session, the client creates links to communicate with specific entities like queues, topics, or other resources provided by the server. Links are bidirectional communication channels used for sending and receiving messages.
The component can work as a sender and receiver node. Allows to create any number of links for each session, up to the limit set in the MaxLinksPerSession property.
Sender Links
To create a new sender link, use the method CreateSenderLink and pass the name of the session and optionally the name of the sender link. If the link is created successfully, the event OnAMQPLinkOpen is raised.
AMQP1.CreateSenderLink('MySession', 'MySenderLink'); procedure procedure TfrmClientAMQP1.AMQP1AMQPLinkOpen(Sender: TObject; const aSession: TsgcAMQP1Session; const aLink: TsgcAMQP1Link; const aAttach: TsgcAMQP1FrameAttach); begin ShowMessage('#link-open: ' + aLink.Name); end;
Receiver Links
To create a new receiver link, use the method CreateReceiverLink and pass the name of the session and optionally the name of the receiver link. If the link is created successfully, the event OnAMQPLinkOpen is raised.
AMQP1.CreateReceiverLink('MySession', 'MyReceiverLink'); procedure procedure TfrmClientAMQP1.AMQP1AMQPLinkOpen(Sender: TObject; const aSession: TsgcAMQP1Session; const aLink: TsgcAMQP1Link; const aAttach: TsgcAMQP1FrameAttach); begin ShowMessage('#link-open: ' + aLink.Name); end;
With the session established and links created, the client can start performing message operations such as sending messages to a destination. Use the method SendMessage to send a message using a sender link.
AMQP1.SendMessage('MySession', 'MySenderLink', 'My first AMQP Message');
By default, the Receiver Links are created in Automatic mode, which means that every time a new message arrives, it will be delivered to the client.
If the Receiver Links has been created in manual mode, use the Sync Method GetMessage to fetch and wait till a new message arrives.
In Automatic and Manual mode, every time a new message arrives, the event OnAMQPMessage is fired.
procedure OnAMQPMessageEvent(Sender: TObject; const aSession: TsgcAMQP1Session; const aLink: TsgcAMQP1ReceiverLink; const aMessage: TsgcAMQP1Message; var DeliveryState: TsgcAMQP1MessageDeliveryState); begin ShowMessage(aMessage.ApplicationData.AMQPValue.Value); end;
Download the AMQP 1.0.0 Client Demo compiled for Windows using the sgcWebSockets library.
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.