Mercure Setup
Start and configure Mercure server
Pimcore Studio supports two Mercure hub lines. Both are covered by the setup below, they differ only in one directive:
| Hub | Docker image | MERCURE_EXTRA_DIRECTIVES |
|---|---|---|
| Mercure 1.1.3 or later (recommended) | dunglas/mercure:v1 | anonymous and protocol_version_compatibility 8 |
| Mercure 0.x (e.g. 0.24) | dunglas/mercure:v0.24 | anonymous only |
For details see the Mercure docs.
This bundle still speaks the Mercure 0.x protocol. A 0.x hub understands it natively. A 1.x hub needs its compatibility mode, enabled with
protocol_version_compatibility 8: it accepts the 0.x tokens, cookie and subscription parameters this bundle uses, and relaxes access token validation for them.
- Do not add
protocol_version_compatibility 8to a 0.x hub: Mercure 0.24 only accepts7and refuses to start.- Do not run a 1.x hub without it: publishing fails with
401 untrusted issuer ""and Studio receives no live updates.- Pin the image to
v1orv0.24instead of usinglatestor an untagged image, so that a future major version cannot break the setup. Mercure no longer ships fixes for 0.x hubs.
Mercure configuration
Following Mercure configuration aspects need to be considered
- Add same JWT key to
MERCURE_PUBLISHER_JWT_KEYandMERCURE_SUBSCRIBER_JWT_KEY - JWT key needs to be minimum 256 bits (or 32 chars) long
- On a Mercure 1.x hub, add
protocol_version_compatibility 8toMERCURE_EXTRA_DIRECTIVES(not on a 0.x hub, see above)
Example Configuration with docker compose
Following there are configuration snippets for a setup with docker compose.
In that case, Mercure is only exposed via nginx reverse proxy that runs under https. Internal communication is handled via http (which is handy when using self-signed certificates in development context).
nginx.conf
Configure reverse proxy in nginx to route Mercure traffic accordingly.
server {
# (...)
location /hub {
proxy_pass http://mercure/.well-known/mercure;
}
# Thumbnails
# (...)
}
docker-compose.yaml
Add Mercure to your docker compose file and make sure to add the configuration for JWT keys and anonymous directives.
# add here all the other necessary containers...
# configure nginx to run under https
nginx:
image: nginx:stable-alpine
ports:
- 443:443
depends_on:
- php
volumes:
- ./studio-demo:/var/www/html:ro
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
# certificates for running nginx (for direct edit to work, system has to run with https)
- ~/.certs:/etc/nginx/certs
mercure:
image: dunglas/mercure:v1
restart: unless-stopped
environment:
# Disable HTTPS
SERVER_NAME: ':80'
MERCURE_PUBLISHER_JWT_KEY: '<your-256-bit-secret-min-32-chars>'
MERCURE_SUBSCRIBER_JWT_KEY: '<your-256-bit-secret-min-32-chars>'
MERCURE_EXTRA_DIRECTIVES: |-
anonymous
protocol_version_compatibility 8
expose:
- "80"
volumes:
- pimcore-demo-mercure-data:/data
- pimcore-demo-mercure-config:/config
volumes:
# ...
# Add volumes for mercure
pimcore-demo-mercure-data:
pimcore-demo-mercure-config:
config.yaml
Configure Pimcore application with JWT key as well as the client side and server side URLs for Mercure.
hub_url_server must be set and has no default; it is not checked at boot, but without it no updates are published.
hub_url_client is optional; if it is not set, the scheme and host of the current request plus /hub is used.
pimcore_studio_backend:
mercure_settings:
jwt_key: '<your-256-bit-secret-min-32-chars>'
hub_url_client: 'https://your-app-domain.com/hub'
hub_url_server: 'http://mercure/.well-known/mercure'
If you would like to retrieve the URLs from within the application, you can use the following methods:
getClientSideUrl()to get the client URLgetServerSideUrl()to get the server URL
These methods are available in the Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UrlServiceInterface class interface.
Check if Mercure is running
To see if Mercure is up and available, call https://your-app-domain.com/hub (or hub_url_client
you configured in configuration). This request should return the text
missing "match" subscription parameter
or
Unauthorized
Also execute
curlagainst thehub_url_serveryou configured (e.g.curl http://mercure/.well-known/mercure) from the command line of the server, inside the PHP container if you use Docker, to ensure that the URL is accessible by the server itself. The public/hubURL is not a substitute: it can answer whilehub_url_serveris unreachable from PHP.
Additional Aspects
Running Mercure with external URL
When running Mercure as external service with external URL, make sure to configure CSP properly.
Mercure Configuration
Sample based on docker compose, important part is the cors_origins directive.
mercure:
image: dunglas/mercure:v1
restart: unless-stopped
environment:
# All other necessary configs ...
# ...
# Add anonymous directives, compatibility mode and cors configuration
MERCURE_EXTRA_DIRECTIVES: |-
anonymous
protocol_version_compatibility 8
cors_origins "https://<YOUR_PIMCORE_URL>"
Webserver Reverse Proxy
When you are using Mercure default URLs, you might need to configure a reverse proxy to make sure the Mercure requests are routed correctly to the Mercure instance.
Also, keep in mind, that communication needs to be HTTPS. Thus, it might be useful to place the Mercure server behind a reverse proxy of the actual webserver (who handles the certificates).
Beware that the development UI is not working when using a reverse proxy. (See open ticket https://github.com/dunglas/mercure/issues/951)
Apache Reverse Proxy
For Apache, for example, enable http_proxy in apache and add the following reverse proxy in your apache config:
ProxyPass /hub http://localhost:3000/.well-known/mercure
ProxyPassReverse /hub http://localhost:3000/.well-known/mercure
Nginx Reverse Proxy
For Nginx use following configuration:
location /hub {
proxy_pass http://mercure/.well-known/mercure;
}
Development UI
The development UI can be helpful to develop new features. It is possible to subscribe and publish messages.
With the configuration below, the development UI is available at http://localhost:8080/.well-known/mercure/debug/.
mercure:
image: dunglas/mercure:v1
restart: unless-stopped
environment:
# All other necessary configs ...
# ...
MERCURE_EXTRA_DIRECTIVES: |-
anonymous
protocol_version_compatibility 8
debugger
ports:
- "8080:80"
debugger only serves the UI, which uses the token you provide, and is safe in production. playground also mints an
all-access token and relaxes the hub's security settings: use it only for local development.
Please note the trailing slash in the URL, without it the UI will not be available.