PHP code example of yevhenlisovenko / nano-service

1. Go to this page and download the library: Download yevhenlisovenko/nano-service 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/ */

    

yevhenlisovenko / nano-service example snippets




use NanoService\NanoPublisher;

$publisher = new NanoPublisher('amqp://guest:guest@localhost:5672');
$publisher->publish('order.created', ['order_id' => 123, 'amount' => 500]);



use NanoService\NanoConsumer;

$consumer = new NanoConsumer('amqp://guest:guest@localhost:5672', 'order.created');
$consumer->consume(function ($message) {
    echo "Received event: ", json_encode($message), "\n";
});

use NanoService\NanoServiceMessage;

$message = new NanoServiceMessage('order.created', ['order_id' => 123, 'amount' => 500]);
echo $message->toJson();

use NanoService\NanoServiceClass;

class OrderService extends NanoServiceClass {
    public function handleMessage($message) {
        echo "Processing Order: ", json_encode($message), "\n";
    }
}

$orderService = new OrderService('amqp://guest:guest@localhost:5672', 'order.created');
$orderService->start();
dockerfile
FROM php:8.1-cli
WORKDIR /app
COPY . .
RUN composer install
CMD ["php", "consumer.php"]