PHP code example of jdwx / redis-pubsub

1. Go to this page and download the library: Download jdwx/redis-pubsub 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/ */

    

jdwx / redis-pubsub example snippets


use JDWX\RedisPubSub\RedisPubSub;

# By default, a connection is made to localhost:6379 without TLS.
$rps = new RedisPubSub();
$rps->publish( 'test', 'Hello, world!' );

use JDWX\RedisPubSub\RedisPubSub;

# TLS is enabled by providing the paths to the client certificate and key, and,
# optionally, the CA certificate if the server should be verified.
$rps = new RedisPubSub( 'localhost', 6379, './client.crt', './client.key', './ca.crt' );

# Perform authentication.
$rps->auth( 'password' );

# Subscribe to a channel.
$rps->subscribe( 'test' );

# This will block until a message is received.
$msg = $rps->recv();

# This will not block if there are no pending messages.
$msg = $rps->tryRecv();

# This will wait for up to 5 seconds for a message to return.
$msg = $rps->tryRecv( 5.0 );

# This will wait for a message for up to 5 seconds without returning it.
$msg = $rps->tryWait( 5.0 );

# A callback function.
function MyCallback( array $msg ) : void {
    var_dump( $msg );
}

# This will pass all messages currently in the queue to the callback function,
# one at a time.
$rps->recvAll( 'MyCallback' );

# This will wait 5 seconds for messages to arrive, passing them one at a time
# to the callback function.
$rps->recvAllWait( 5.0, 'MyCallback' );