0

Can't do publish to mercure inside symfony controller, but I can do it from postMan and from demo mercure URL http://localhost:3000/.well-known/mercure/ui/

From class PublishController extends AbstractController I have:

 $hub->publish($update); -

 TransportException
Failed to connect to localhost port 3000 after 0 ms: Connection refused for "http://localhost:3000/.well-known/mercure".

docker-compose.yaml

    mercure:
    image: dunglas/mercure
    restart: unless-stopped
    environment:
      SERVER_NAME: ':80'
      DEMO: 1
      PUBLISH_ALLOWED_ORIGINS: '*'
      MERCURE_CORS_ALLOWED_ORIGINS: '*'
      ALLOW_ANONYMOUS: 1
      MERCURE_EXTRA_DIRECTIVES: |-
        cors_origins "http://localhost:8000"
        anonymous
      MERCURE_PUBLISHER_JWT_KEY: '!ChangeMe!'
      MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeMe!'
    # Comment the following line to disable the development mode
    command: /usr/bin/caddy run -config /etc/caddy/Caddyfile.dev

In env file:

MERCURE_URL=http://localhost:3000/.well-known/mercure
# The public URL of the Mercure hub, used by the browser to connect
MERCURE_PUBLIC_URL=http://localhost:3000/.well-known/mercure
MERCURE_PUBLISH_URL=http://mercure:3000/.well-known/mercure
# The secret used to sign the JWTs
MERCURE_JWT_SECRET=!ChangeMe!
    enter code here

Webserver URL http://localhost:8000 and I can even subscribe on events from front-end

<script>
const eventSource = new EventSource("{{ mercure('topic')|escape('js') }}");
eventSource.onmessage = event => {
    // Will be called every time an update is published by the server
    console.log(JSON.parse(event.data));
}

How to handle it?

Den Step
  • 1
  • 1
  • `localhost` have different semantics inside a container and in the host machine. From inside the symfony container you should use the `MERCURE_PUBLISH_URL` that points to the `mercure` host. Does the service `$hub` use that url? – msg Jul 10 '22 at 16:18
  • Hub use MERCURE_PUBLIC_URL, I indicate this url http://localhost:3000/.well-known/mercure for whole cases @msg – Den Step Jul 10 '22 at 16:31
  • I have tried to indicate this one as well http://mercure:3000/.well-known/mercure (like a name of container) but still have an error – Den Step Jul 10 '22 at 16:43
  • I found the solution, We need to indicate as mercure container name host, so for me it`s just mercure/.well-known/mercure – Den Step Jul 10 '22 at 16:54

0 Answers0