PHP code example of xp-forge / stomp

1. Go to this page and download the library: Download xp-forge/stomp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xp-forge / stomp example snippets


use peer\stomp\{Connection, SendableMessage};

$conn= new Connection('stomp://localhost:61613/');
$conn->connect();

$conn->getDestination('/queue/producer')->send(
  new SendableMessage('Message contents', 'text/plain')
);

use peer\stomp\{Connection, Subscription};

$conn= new Connection('stomp://localhost:61613/');
$conn->connect();

$conn->subscribeTo(new Subscription('/queue/producer', function($message) {
  Console::writeLine('---> Received message: ', $message);
  $message->ack();
}));

$conn->consume();

use peer\stomp\{Connection, Subscription, Failover};

$nodes= ['stomp://one.example.com:61613/', 'stomp://two.example.com:61613/'];

// Connect randomly to one or the other
$conn= new Connection(Failover::using($nodes)->byRandom());
$conn->connect();

$conn->subscribeTo(new Subscription('/queue/producer', function($message) {
  Console::writeLine('---> Received message: ', $message);
  $message->ack();
}));

$conn->consume();