Skip to content

Commit

Permalink
Merge pull request #150 from jeromegamez/client-properties-support
Browse files Browse the repository at this point in the history
Add support for setting client properties
  • Loading branch information
WyriHaximus authored May 20, 2024
2 parents b4d6781 + e657b54 commit 873ec47
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ Note: invalid SSL configuration will cause connection failure.

See also [common configuration variants](examples/ssl/).

### Providing client properties

Client Connections can [present their capabilities](https://www.rabbitmq.com/connections.html#capabilities) to
a server by presenting an optional `client_properties` table when establishing a connection.

For example, a connection name may be provided by setting the
[`connection_name` property](https://www.rabbitmq.com/connections.html#client-provided-names):

```php
$connection = [
'host' => 'HOSTNAME',
'vhost' => 'VHOST', // The default vhost is /
'user' => 'USERNAME', // The default user is guest
'password' => 'PASSWORD', // The default password is guest
'client_properties' => [
'connection_name' => 'My connection',
],
];

$bunny = new Client($connection);
$bunny->connect();
```

### Publish a message

Now that we have a connection with the server we need to create a channel and declare a queue to communicate over before we can publish a message, or subscribe to a queue for that matter.
Expand Down
10 changes: 8 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,17 @@ public function __construct(array $options = [])
unset($options['heartbeat_callback']);
}


if (isset($options['ssl']) && is_array($options['ssl'])) {
$options['tls'] = $options['ssl'];
}

if (!isset($options['client_properties'])) {
$options['client_properties'] = [];
}

if (!is_array($options['client_properties'])) {
throw new \InvalidArgumentException('Client properties must be an array');
}

$this->options = $options;
$this->connector = new Connector($this->options);
Expand Down Expand Up @@ -233,7 +239,7 @@ protected function authResponse(MethodConnectionStartFrame $start): void
], $responseBuffer);
$responseBuffer->discard(4);

$this->connection->connectionStartOk($responseBuffer->read($responseBuffer->getLength()), [], 'AMQPLAIN', 'en_US');
$this->connection->connectionStartOk($responseBuffer->read($responseBuffer->getLength()), $this->options['client_properties'], 'AMQPLAIN', 'en_US');
}

/**
Expand Down
9 changes: 9 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public function testConnect()
$this->assertFalse($client->isConnected());
}

public function testConnectWithInvalidClientProperties()
{
$this->expectException(\InvalidArgumentException::class);

$this->helper->createClient([
'client_properties' => 'not an array'
]);
}

public function testConnectFailure()
{
$this->expectException(ClientException::class);
Expand Down

0 comments on commit 873ec47

Please sign in to comment.