Asynchronous Message Exchange between Delphi and PHP Web Applications

New examples in Habari ActiveMQ Client release 2.4 show how Delphi (and Free Pascal) applications can use Apache ActiveMQ to exchange messages with PHP web applications.

First example: publish a message for every HTTP GET request

Using the open source PHP Stomp client from FuseSource, sending messages requires only a couple lines of code.

This example writes a text message to the queue TOOL.DEFAULT:

<?php
// include Stomp library
 require_once("Stomp.php");
// make a connection
 $con = new Stomp("tcp://127.0.0.1:61613");
// connect
 $con->connect();
// send a message to the queue
 $con->send("/queue/TOOL.DEFAULT", "test");
 echo "Sent message with body 'test'\n";
// disconnect
 $con->disconnect();
>

The demo application ConsumerTool.exe then can be used to consumer the message:

Second example: publish a message using form data of a HTTP POST request

A web form can be used to enter a custom message text. The form then sends the text to the submit.php script as a HTTP POST request.

<form method="post" action="submit.php">
    Message: <br>
    <textarea name="msg" cols="40" rows="5">
Enter some text here for the message body...
    </textarea><br>
    <input type="submit" name="Button" >
</form>

The PHP code in submit.php:

<?php
// include Stomp library
  require_once("Stomp.php");
// make a connection
  $con = new Stomp("tcp://127.0.0.1:61613");
// connect
  $con->connect();
// send a message to the queue
  $con->send("/queue/TOOL.DEFAULT", $_POST[msg]);
  echo "Sent message with body: '$_POST[msg]'\n";
// disconnect
  $con->disconnect();
  ?>
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s