ratchetphp/Ratchet · error · InvalidArgumentException

Argument #3 ($loop) expected…

Error message

Argument #3 ($loop) expected null|React\EventLoop\LoopInterface

What it means

This is an argument type/count validation error raised in IoServer::__construct() when the third argument ($loop) is not null and not an instance of React\EventLoop\LoopInterface. IoServer requires a loop either injected directly or, when null, one resolved later via factory()/run(); passing any other value (e.g. a string, a closed loop of the wrong type, or an extra positional argument) means the server cannot be constructed.

Solutions

  1. Pass an instance of React\EventLoop\LoopInterface (e.g. React\EventLoop\Loop::get() on event-loop v1.2+, or Factory::create() on older versions) as the third argument.
  2. Pass explicit null as $loop if you intend the server to use a default loop resolved internally.
  3. Check the call site for argument order — the loop must come after $app and $socket, and no additional arguments should be passed.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at src/Ratchet/Server/IoServer.php:40 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ratchetphp/Ratchet@e621c6c40b (2026-09-16). Data as JSON: /api/errors/2cbe6970019e245c. Report an issue: GitHub.

Appendix: source

Thrown at src/Ratchet/Server/IoServer.php:40

    /**
     * @var \Ratchet\MessageComponentInterface
     */
    public $app;

    /**
     * The socket server the Ratchet Application is run off of
     * @var \React\Socket\ServerInterface
     */
    public $socket;

    /**
     * @param \Ratchet\MessageComponentInterface  $app      The Ratchet application stack to host
     * @param \React\Socket\ServerInterface       $socket   The React socket server to run the Ratchet application off of
     * @param \React\EventLoop\LoopInterface|null $loop     The React looper to run the Ratchet application off of
     */
    public function __construct(MessageComponentInterface $app, ServerInterface $socket, $loop = null) {
        if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
            throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
        }

        if (false === strpos(PHP_VERSION, "hiphop")) {
            gc_enable();
        }

        set_time_limit(0);
        ob_implicit_flush();

        $this->loop = $loop;
        $this->app  = $app;
        $this->socket = $socket;

        $socket->on('connection', array($this, 'handleConnect'));
    }

    /**
     * @param  \Ratchet\MessageComponentInterface $component  The application that I/O will call when events are received

View on GitHub (pinned to e621c6c40b)