Centrifugo client "connection refused" in CI tests
A Centrifuge/Centrifugo client raises a connection error with "connection refused" when it dials the server before it is accepting connections. The realtime server, started as a service, was not ready when the test connected.
What this error means
The client's error handler fires with a message containing "connection refused" (or "dial tcp ... connect: connection refused"). It flakes depending on whether Centrifugo finished starting first.
error: dial tcp 127.0.0.1:8000: connect: connection refused
centrifuge: connection error, will reconnectCommon causes
The Centrifugo service was not ready
Centrifugo, started as a CI service container or background process, had not bound its port when the test client connected.
A wrong host, port, or scheme in the test client
Connecting to the wrong port, or ws:// where the server expects a different path, refuses the connection.
How to fix it
Wait for Centrifugo before connecting
- Poll the Centrifugo health or connection endpoint until it responds.
- Only start the client once the port accepts connections.
- Confirm the URL, port, and path match the server config.
npx wait-on http://localhost:8000/health && npm testUse the correct connection endpoint
Point the client at the server's WebSocket endpoint exactly as configured.
const client = new Centrifuge('ws://localhost:8000/connection/websocket');How to prevent it
- Poll Centrifugo health before connecting in tests.
- Keep the client URL and path in sync with the server config.
- Start realtime service containers with a readiness gate.