How to notify user using ->messenger() and ajax?
This works for me:
$element = NULL;
if (!empty($triggering_element['#ajax']['element'])) {
  $element = \Drupal\Component\Utility\NestedArray::getValue($form, $triggering_element['#ajax']['element']);
}
// Element not specified or not found. Show messages on top of the form.
if (!$element) {
  $element = $form;
}
$response = new \Drupal\Core\Ajax\AjaxResponse();
$response-&gt;addCommand(new \Drupal\Core\Ajax\ReplaceCommand('[data-drupal-selector=&quot;' . $form['#attributes']['data-drupal-selector'] . '&quot;]', $form));
$response-&gt;addCommand(new \Drupal\Core\Ajax\PrependCommand('[data-drupal-selector=&quot;' . $element['#attributes']['data-drupal-selector'] . '&quot;]', ['#type' =&gt; 'status_messages']));
return $response;