STOMP - SSL - PHP
Updated on 20 Sep 2023
I am going to use the stomp-php library from packagist.
- I am referencing the SSL certificate and ca-certificate files that we created earlier on lines 14, 15.
- I am using a slightly different technique for connection so that I can pass in the certificate files
- this example also uses a different approach by having the user/pass in the rabbitmq.conf file, so I don’t need to specify it in the code.
<?php
require __DIR__ . '/vendor/autoload.php';
use Stomp\Client;
use Stomp\SimpleStomp;
use Stomp\Network\Connection;
use Stomp\Transport\Message;
//--https://www.php.net/manual/en/context.ssl.php
$opts = [
'ssl' => [
//'local_cert' => '/etc/rabbitmq/ssl/server_server4_certificate.pem',
'cafile' => '/etc/rabbitmq/ssl/ca_certificate.pem',
'local_pk' => '/etc/rabbitmq/ssl/server_server4_key.pem',
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
// make a connection
//--61613 - normal
//--61614 - encrypted
//$client = new Client('tcp://localhost:61613');
//--can use either tls or ssl
$connection = new Connection('tls://localhost:61614', 1, $opts);
$client = new Client($connection);
/*
I can have this set in the /etc/rabbitmq/rabbitmq.conf file.
stomp.default_user = demo_user
stomp.default_pass = demo_pass
and I wont have to call setLogin(...)
*/
//$client->setLogin('demo_user', 'demo_pass');
$client->setVhostname('demo_host');
$stomp = new SimpleStomp($client);
// send a message to the queue
$body = 'test message from php';
$stomp->send('/queue/q1', new Message('test message from php - ssl' . "\n"));
?>
Sending the message
This is how I sent the message using the command line.
php send.php
Rabbitmqadmin
I can use rabbitmqadmin to check the message
rabbitmqadmin list vhosts
And to see the message, I can use the following command.
rabbitmqadmin get queue=q1 --vhost=demo_host -u guest -p guest
I am using guest username because I have assigned the appropriate privileges to that account.