John Siu Blog

Tech - Business Tool, Personal Toys

RabbitMQ PHP-AMQP RPC - Setup

☰ Table of Content

This is Part 3 of PHP-AMQP RPC implementation.

Series Content

  1. Client
  2. Server
  3. Setup &amp; Include <- You are here

This part give you the include file and RabbitMQ setup I am using.

PHP-AMQP RPC Setup

JS-amqp-setup.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

include (__DIR__ .'/JS-amqp-include.php');

/*
$co = new AMQPConnection(array('host' => HOST, 'port' => PORT, 'vhost' => VHOST, 'login' => USER, 'password' => PASS));
 */
$co = new AMQPConnection();
$co->setHost(HOST);
$co->setLogin(USER);
$co->setPassword(PASS);
$co->setVHost(VHOST);
$co->connect();

$ch = new AMQPChannel($co);

$ex = new AMQPExchange($ch);
$ex->setName(X_DIR);
$ex->setType(XT_DIR);
$ex->setFlags(AMQP_DURABLE);
$ex->declareExchange();

$q_txt = new AMQPQueue($ch);
$q_txt->setName(Q_TXT);
$q_txt->setFlags(AMQP_DURABLE);
$q_txt->declareQueue();

$q_txt->bind(X_DIR, '');

$q_rpc = new AMQPQueue($ch);
$q_rpc->setName(Q_RPC);
$q_rpc->setFlags(AMQP_DURABLE);
$q_rpc->declareQueue();

$q_rpc->bind(X_DIR, K_RPC);

echo $ex->getType();

//$ch->close();
$co->disconnect();

?>

PHP-AMQP RPC Include

JS-amqp-include.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
define('HOST', 'rmq-sevr');
define('PORT', 5672);
define('USER', 'rmq-user');
define('PASS', 'rmq-pass');
define('VHOST', 'rmq-vhost');

// Name
define('X_DIR', 'X_DIR');
define('Q_TXT', 'Q_TXT');
define('Q_RPC', 'Q_RPC');
define('K_RPC', 'K_RPC');

// Content Type
define('CT_JSON', 'application/json');
define('CT_TEXT', 'text/plain');

// Exchange Type
define('XT_DIR', 'direct');
define('XT_FAN', 'fanout');
define('XT_TOP', 'topic');
?>

John Siu

Update: 2020-08-19
comments powered by Disqus