ratchetphp/Ratchet · error · InvalidArgumentException
Argument #4 ($loop) expected…
Error message
Argument #4 ($loop) expected null|React\EventLoop\LoopInterface
What it means
Ratchet\App's constructor performs a manual runtime type check on its fourth argument because the code must support legacy PHP versions that predate nullable type hints. Passing $loop that is neither null nor a React\EventLoop\LoopInterface instance triggers this InvalidArgumentException. $loop is normally null, letting Ratchet pick the default loop via React\EventLoop\Loop::get() or the legacy Factory.
Solutions
- Pass an instance of React\EventLoop\LoopInterface (e.g. React\EventLoop\Loop::get()) as the 4th argument, or omit it entirely so Ratchet creates the default loop.
- If sharing a loop with other reactphp components, create it once via Loop::get() and pass that same instance everywhere.
- Check argument order: __construct($httpHost, $port, $address, $loop, $context) — ensure no earlier positional argument shifted.
- Replace legacy \React\EventLoop\Factory::create() calls with Loop::get() when on reactphp/event-loop v1.2+.
Example fix
// before
$app = new Ratchet\App('0.0.0.0', 8080, '0.0.0.0', 'default');
// after
$app = new Ratchet\App('0.0.0.0', 8080, '0.0.0.0', React\EventLoop\Loop::get()); Defensive patterns
Strategy: type-guard
Validate before calling
$loop = $loop ?? \React\EventLoop\Loop::get();
if (!$loop instanceof \React\EventLoop\LoopInterface) {
throw new \TypeError('App() $loop must be a React\EventLoop\LoopInterface or null');
} Type guard
function isReactLoop($loop): bool {
return $loop === null || $loop instanceof \React\EventLoop\LoopInterface;
} Try / catch
try {
$app = new Ratchet\App('localhost', 8080, '127.0.0.1', $loop);
} catch (\InvalidArgumentException $e) {
$app = new Ratchet\App('localhost', 8080); // fall back to default loop
} Prevention
- Omit the $loop argument unless you specifically share a loop across components.
- Use React\EventLoop\Loop::get() as the canonical loop instance on reactphp/event-loop v1.2+.
- Check positional argument order ($httpHost, $port, $address, $loop, $context) when passing by position.
- Use named arguments in PHP 8+ to avoid positional mix-ups.
When it happens
Trigger: Passing a string like 'default' or 'ev' as the 4th constructor argument (a common confusion with old docs); passing a different event loop implementation such as a plain Swoole or Amp loop that does not implement LoopInterface; misordering positional arguments so something else lands in $loop.
Common situations: Upgrading reactphp/event-loop across major versions and keeping a stale loop factory call whose return type changed; copy-paste of constructor snippets from blogs using old signatures; attempting to share a loop from another async framework.
Related errors
- Argument #4 ($serializer) expected…
- $request can not be null
- All routes must implement Ratchet\Http\HttpServerInterface
- Invalid domain
- Invalid Port
AI-assisted analysis of ratchetphp/Ratchet@e621c6c40b (2026-09-16).
Data as JSON: /api/errors/5bcc9b5aed69c8dd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Ratchet/App.php:72
/**
* @var int
*/
protected $_routeCounter = 0;
/**
* @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');`
* @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843
* @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine.
* @param ?LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you.
* @param array $context
*/
public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', $loop = null, $context = array()) {
if (extension_loaded('xdebug') && getenv('RATCHET_DISABLE_XDEBUG_WARN') === false) {
trigger_error('XDebug extension detected. Remember to disable this if performance testing or going live!', E_USER_WARNING);
}
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #4 ($loop) expected null|React\EventLoop\LoopInterface');
}
if (null === $loop) {
// prefer default Loop (reactphp/event-loop v1.2+) over legacy \React\EventLoop\Factory
$loop = class_exists('React\EventLoop\Loop') ? Loop::get() : LegacyLoopFactory::create();
}
$this->httpHost = $httpHost;
$this->port = $port;
// prefer SocketServer (reactphp/socket v1.9+) over legacy \React\Socket\Server
$socket = class_exists('React\Socket\SocketServer') ? new SocketServer($address . ':' . $port, $context, $loop) : new LegacySocketServer($address . ':' . $port, $loop, $context);
$this->routes = new RouteCollection;
$this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop);
$policy = new FlashPolicy;
$policy->addAllowedAccess($httpHost, 80);View on GitHub (pinned to e621c6c40b)