We continue our master class on introducing asynchronous operations in VtigerCRM. In previous episodes we learned how to send data from Vtiger to queue and receive it in Laravel. Now let's look at the opposite scenario - we will read the messages in the queue and process them accordingly.
This is probably the most difficult stage in working with queues. Previously, everything was quite simple - we wrote handlers, they sent information about the event to RabbitMQ. But now we need to write a special script that will constantly listen queue and it is desirable that it can be managed not by cron, but by Supervisor.
The picture above shows a diagram of how our interaction with the queue will be built. RabbitMQ offers two ways to receive messages: we can request them ourselves from the application using the basic.get method, or the server can send them to us using the basic.consume method. The picture above shows just the second approach, which we will use.
The subscribe method we call adds the application to the queue and subscribes it to new messages.
When developing your application, make sure that the recipient is registered with the basic.consume method and not with basic.get. The second method is quite expensive and requires significant server resources.
With the subscription approach, messages are sent to the client from the broker as soon as they are available and the client is available to receive them. This makes it very fast and easy to process messages. In addition, using the subscribe approach means that the receiver is connected as long as the channel exists or until the client itself closes the connection.
Below you can see a detailed tutorial on how we can implement listening for messages in RabbitMQ and how we will handle them using the example of creating and updating contacts.
Before watching the video, make sure you read my previous posts and watch the introductory videos:
- How to install RabbitMQ
- How to connect RabbitMQ with VtigerCRM? How do I send data to the queue?
- How to update data in another application from Vtiger?
You almost done with this video course. Let's try to make vtiger async!