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
Here and further examples in PHP will be given. The examples show the use of the library for the client.
use SimQ\Producer;
$producer = new Producer( 'localhost', 4012, 'groupName', 'channelName', 'login', 'password' );
$producer->sendMessage( 'test' );
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();
}
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:
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 );
On the consumer side, it is necessary to find out the UUID of the message and delete it on the slave server.
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() );
}
For synchronization, you can clear the queue. Only the owner of the group can clear the queue.
use SimQ\Group;
$group = new Group( 'localhost', 4012, 'groupName', 'password' );
$group->clearQ( 'channelName' );