PHP code example of jeeinn / php-stomp-frame

1. Go to this page and download the library: Download jeeinn/php-stomp-frame 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/ */

    

jeeinn / php-stomp-frame example snippets


$userName = 'test';
$password = 'passcode';
$queue = 'service_queue_v1.2';

$stompFrame = new \Stomp\Frame();

# connect frame
$connectFrame = $stompFrame->setLogin($userName, $password)->setHeartBeat(0, 10000)->getConnect();
print_r($connectFrame);

# subscribe frame
$subscribeFrame = $stompFrame->getSubscribe($queue);
print_r($subscribeFrame);

# send frame
$sendFrame = $stompFrame->setBody('i am tester')->getSend($queue);

$message = <<<Message
CONNECTED
server:ActiveMQ/5.11.0.redhat-630495
heart-beat:10000,0
session:ID:broker-amq-2-3-2tx5b-40311-1621354806699-3:162340
version:1.1

Message;

$stompFrame = new \Stomp\Frame();
try {
    $parsed = $stompFrame->parser($message);
    print_r($parsed);
} catch (Exception $e) {
    echo $e->getMessage();
}

// over wss
s://echo.websocket.org:443';
$userName = 'test';
$password = 'passcode';
$queue = 'service_queue_v1.2';

$stompFrame = new \Stomp\Frame();
# connect frame
$connectFrame = $stompFrame->setLogin($userName, $password)->setHeartBeat(0, 10000)->getConnect();
# subscribe frame
$subscribeFrame = $stompFrame->getSubscribe($queue);

#use websocket
$client = new \WebSocket\Client($url);
$client->text($connectFrame);
//var_dump($client->isConnected());
$client->text($subscribeFrame);

# loop listening
while (true) {
    try {
        $message = $client->receive();
        $parsed = $stompFrame->parser($message);
        //print_r($parsed);
        # Error, Break while loop to stop listening, Possibly log errors
        if ($parsed['command'] == 'ERROR') {
            echo $parsed['body'];
            $client->close();
            break;
        }
        // Deal your data
        $data = json_decode($parsed['body'], true);
        print_r($data);
        // Act[enter image description here][4] on received message
        // Later, Break while loop to stop listening
    } catch (Exception $e) {
        // Possibly log errors
        echo $e->getMessage();
    }
}