Skip to content
Latchkey

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.

Centrifugo
error: dial tcp 127.0.0.1:8000: connect: connection refused
  centrifuge: connection error, will reconnect

Common 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

  1. Poll the Centrifugo health or connection endpoint until it responds.
  2. Only start the client once the port accepts connections.
  3. Confirm the URL, port, and path match the server config.
Terminal
npx wait-on http://localhost:8000/health && npm test

Use the correct connection endpoint

Point the client at the server's WebSocket endpoint exactly as configured.

test.mjs
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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →