mercure icon indicating copy to clipboard operation
mercure copied to clipboard

Connection to Mercure lost on container restart – how to handle auto-reconnect or refresh?

Open gremo opened this issue 7 months ago • 1 comments

Hi there 👋

I believe this is a fairly common issue, and I’m hoping to find some guidance here, as I couldn’t find anything relevant in the documentation.

In my application, I'm using turbo_stream_listen to establish a connection to a Mercure hub and update the page in real-time when specific events occur. Everything works great so far. The whole stack runs inside Docker Compose using FrankenPHP (and a custom Caddyfile to enable Mercure).

However, I’m running into a problem during deployment:

docker compose up -d --wait --remove-orphans

Deploy

When Docker restarts the containers, the client’s connection to the Mercure server is lost.

In a normal user-driven scenario, this wouldn’t be a big issue — as soon as the user interacts with the app again, a page load or navigation would re-establish the connection seamlessly.

But in my case, the app runs unattended on a tablet in kiosk mode. So after each deployment, the page stays open but no longer receives updates, and I’m forced to ask the client to manually refresh the page, which is not ideal.

I thought about sending a Turbo Stream to trigger a client-side refresh, but of course that doesn’t work — by the time the message is sent, the Mercure connection is already lost.

Is there a recommended way to detect a broken Mercure connection and trigger a page reload or reconnection automatically? Any idea would be greatly appreciated!

Thanks in advance 🙏

gremo avatar May 16 '25 09:05 gremo

Any updates or thoughts on this? In the meantime, I'm listening for connection drops and refreshing the page once I get a 200 response. It seems to work, but it's not an ideal solution — having a built-in way to handle this would be much better.

gremo avatar Jun 19 '25 18:06 gremo