Only producers can send messages to the channel, and only consumers can extract them. Simultaneous connection of producers and consumers is not limited to each channel.

The minimum message size in the channel can be 1 byte, and the maximum is 4 294 967 295 bytes. Maximum number of messages in a channel - 4 294 967 295 . Each message has a UUIDv4 unique within the channel. The schemes of sending and retrieving messages are shown in the image

Sending messages

If an error occurs during the sending process, the message on the server is not saved in the channel.

Here and further examples in PHP will be given. The examples show the use of the library for the client.

Simple message sending
Sending a message from a path
Sending a message from a file

use SimQ\Producer;

$producer = new Producer( 'localhost', 4012, 'groupName', 'channelName', 'login', 'password' );
$producer->sendMessage( 'test' );

Receiving messages

If an error occurs in the process of receiving a message, it will be returned back to the channel and transmitted to the first consumer who requested the message.
After receiving the message, you need to delete it. If this is not done, the message will be returned back to the channel.
Simple message receiving
Receiving a message to path
Receiving a message to file

use SimQ\Consumer;

$consumer = new Consumer( 'localhost', 4012, 'groupName', 'channelName', 'login', 'password' );
$msg = $consumer->popMessage( 1 );
// 1 - is duration in seconds, maximum - 120, by default 0
// if no new message arrives in the channel after the specified time, an empty response will be returned
if( $msg->isEmpty() ) {
    echo 'No message';
} else {
    echo $msg->getData();
    $consumer->removeMessage();
}

Message replication

From the producer's side

SimQ provides for message replication. As mentioned above, each message contains a UUID. In order to replicate a message from the manufacturer, you need to:

create a message header and get its UUID from the server
send a message to the slave server with the received UUID
send a message to the master server
Simple replication
From path
From file

use SimQ\Producer;

$master = new Producer( '192.168.0.1', 4012, 'groupName', 'channelName', 'login', 'password' );
$slave = new Producer( '192.168.0.2', 4012, 'groupName', 'channelName', 'login', 'password' );

$data = 'text';
$length = strlen( $data );

$master->createMessage( $length );
$slave->createReplicateMessage( $length, $master->getUUID() );
$slave->pushMessage( $data );
$master->pushMessage( $data );

From the consumer's side

On the consumer side, it is necessary to find out the UUID of the message and delete it on the slave server.

Example

use SimQ\Consumer;

$master = new Consumer( '192.168.0.1', 4012, 'groupName', 'channelName', 'login', 'password' );
$slave = new Consumer( '192.168.0.2', 4012, 'groupName', 'channelName', 'login', 'password' );

$msg = $master->popMessage();

if( $msg->isEmpty() ) {
    echo 'No message';
} else {
    echo $msg->getData();
    $master->removeMessage();
    $slave->removeMessageByUUID( $msg->getUUID() );
}

Clearing queues

For synchronization, you can clear the queue. Only the owner of the group can clear the queue.

Example

use SimQ\Group;

$group = new Group( 'localhost', 4012, 'groupName', 'password' );
$group->clearQ( 'channelName' );
2023 © i@trusow.ru