This configuration solved the problem:
RewriteEngine On
RewriteCond %{REQUEST_URI} /mattermost/api/v[0-9]+/(users/)?websocket [NC,OR]
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule "^(/mattermost/.*)" ws://localhost:8065%{REQUEST_URI} [P,QSA,L]
<Location /mattermost>
Require all granted
ProxyPass http://localhost:8065/mattermost
ProxyPassReverse http://localhost:8065/mattermost
ProxyPassReverseCookieDomain localhost fqdn
</Location>