2018-02-27 08:08:31 +00:00
|
|
|
|
---
|
|
|
|
|
title: Queuing Private Messages in Drupal 8
|
2020-03-08 14:32:13 +00:00
|
|
|
|
date: 2018-02-27
|
2018-12-31 12:13:05 +00:00
|
|
|
|
excerpt: Introducing the Private Message Queue module for Drupal 8.
|
2018-02-27 08:08:31 +00:00
|
|
|
|
tags:
|
2020-03-08 17:52:59 +00:00
|
|
|
|
- drupal
|
|
|
|
|
- drupal-8
|
|
|
|
|
- drupal-modules
|
|
|
|
|
- drupal-planet
|
|
|
|
|
- open-source
|
2018-02-27 08:08:31 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
My current project at [Microserve][0] is a Drupal 8 website that uses the
|
|
|
|
|
[Private Message][1] module for users to send messages to each other.
|
|
|
|
|
|
|
|
|
|
In some cases though, the threads could contain hundreds of recipients so I
|
|
|
|
|
decided that it would be good to queue the message requests so that they can be
|
|
|
|
|
processed as part of a background process for better performance. The Private
|
|
|
|
|
Message module does not include this, so I've written and released a separate
|
|
|
|
|
[Private Message Queue][2] module.
|
2018-02-27 21:17:30 +00:00
|
|
|
|
|
2018-02-27 08:08:31 +00:00
|
|
|
|
## Queuing a Message
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The module provices a `PrivateMessageQueuer` service
|
|
|
|
|
(`private_message_queue.queuer`) which queues the items via the `queue()`
|
|
|
|
|
method.
|
2018-02-27 08:08:31 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The method accepts an array of `User` objects as the messsage recipients, the
|
|
|
|
|
message body text and another user as the message owner. (I’m currently
|
|
|
|
|
considering [whether to make the owner optional][4], and default to the current
|
|
|
|
|
user if one is not specified)
|
2018-02-27 08:08:31 +00:00
|
|
|
|
|
|
|
|
|
Here is an example:
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$recipients = $this->getRecipients(); // An array of User objects.
|
|
|
|
|
$message = 'Some message text';
|
|
|
|
|
$owner = \Drupal::currentUser();
|
|
|
|
|
|
|
|
|
|
$queuer = \Drupal::service('private_message_queue.queuer');
|
|
|
|
|
$queuer->queue($recipients, $message, $owner);
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
These three pieces of data are then saved as part of the queued item. You can
|
|
|
|
|
see these by checking the "queue" table in the database or by running
|
|
|
|
|
`drush queue-list`.
|
2018-02-27 08:08:31 +00:00
|
|
|
|
|
2018-09-04 18:58:54 +00:00
|
|
|
|
![](/images/blog/private-message-queue.png)
|
2018-02-27 08:59:28 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ drush queue-list
|
|
|
|
|
Queue Items Class
|
|
|
|
|
private_message_queue 19 Drupal\Core\Queue\DatabaseQueue
|
|
|
|
|
```
|
2018-02-27 08:08:31 +00:00
|
|
|
|
|
|
|
|
|
## Processing the Queue
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The module also provides a `PrivateMessageQueue` queue worker, which processes
|
|
|
|
|
the queued items. For each item, it creates a new private message setting the
|
|
|
|
|
owner and the message body.
|
2018-02-27 08:08:31 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
It uses the `PrivateMessageThread` class from the Private Message module to find
|
|
|
|
|
for an existing thread for the specified recipients, or creates a new thread if
|
|
|
|
|
one isn't found. The new message is then added to the thread.
|
2018-02-27 08:08:31 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The queue is processed on each cron run, so I recommend adding a module like
|
|
|
|
|
[Ultimate Cron][3] so that you can process the queued items frequently (e.g.
|
|
|
|
|
every 15 minutes) and run the heavier tasks like checking for updates etc less
|
|
|
|
|
frequently (e.g. once a day).
|
2018-02-27 08:08:31 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
You can also process the queue manually with Drush using the
|
|
|
|
|
`drush queue-run <queue-name>` command - e.g.
|
|
|
|
|
`drush queue-run private_message_queue`.
|
2018-02-27 08:59:28 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ drush queue-run private_message_queue
|
|
|
|
|
Processed 19 items from the private_message_queue queue in 3.34 sec.
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-27 23:02:37 +00:00
|
|
|
|
[0]: {{site.companies.microserve.url}}
|
2018-02-27 21:17:30 +00:00
|
|
|
|
[1]: https://www.drupal.org/project/private_message
|
|
|
|
|
[2]: https://www.drupal.org/project/private_message_queue
|
|
|
|
|
[3]: https://www.drupal.org/project/ultimate_cron
|
|
|
|
|
[4]: https://www.drupal.org/project/private_message_queue/issues/2948233
|