Ratchet: cant init ratchet

Created on 4 Feb 2017  路  6Comments  路  Source: ratchetphp/Ratchet

Argument 2 passed to ReactSocketServer::__construct() must implement interface ReactEventLoopLoopInterface, none given, called in /var/www/dyntest.dk/php/class/third-party/ratchet/Server/IoServer.php on line 69 /var/www/dyntest.dk/php/class/third-party/react/socket/Server.php(84)

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct(){
        $this->clients = new SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn){
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg){
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s'."\n", $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach($this->clients as $client){
            if($from !== $client){
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn){
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, Exception $e){
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}

$server = Ratchet\Server\IoServer::factory(new Chat, 9000);
$server->run();

Most helpful comment

The "Argument 2 passed to ReactSocketServer::__construct() must implement interface ReactEventLoopLoopInterface, none given" error has been resolved by making changes to my source code:

Old:

$webSock = new React\Socket\Server($loop);
$webSock->listen(88, '0.0.0.0');

New:

$webSock = new React\Socket\Server('0.0.0.0:88',$loop);

All 6 comments

+1

What the output of the command composer show?

I have a similar problem with php7

composer show:

cboden/ratchet v0.4 PHP WebSocket library
cosenary/instagram v2.3 An easy-to-use PHP Class for accessing Instagram's API.
evenement/evenement v2.1.0 脡v茅nement is a very simple event dispatching library for PHP
firebase/php-jwt v4.0.0 A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.
google/apiclient v2.2.0 Client library for Google APIs
google/apiclient-services v0.27 Client library for Google APIs
google/auth v1.0.1 Google Auth Library for PHP
google/cloud v0.39.2 Google Cloud Client Library
google/gax 0.23.0 Google API Extensions for PHP
google/proto-client 0.23.0 Generated proto and gRPC classes for Google Cloud Platform in PHP
google/protobuf v3.4.1 proto library for PHP
grpc/grpc 1.6.0 gRPC library for PHP
guzzle/guzzle v3.9.3 PHP HTTP client. This library is deprecated in favor of https://packagist.org/packages/guzzlehttp/guzzle
guzzlehttp/guzzle 6.3.0 Guzzle is a PHP HTTP client library
guzzlehttp/promises v1.3.1 Guzzle promises library
guzzlehttp/psr7 1.4.2 PSR-7 message implementation that also provides common utility methods
jaybizzle/crawler-detect v1.2.52 CrawlerDetect is a PHP class for detecting bots/crawlers/spiders via the user agent
mikemccabe/json-patch-php 0.1.0 Produce and apply json-patch objects
mobiledetect/mobiledetectlib 2.8.26 Mobile_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
monolog/monolog 1.23.0 Sends your logs to files, sockets, inboxes, databases and various web services
paragonie/random_compat v2.0.11 PHP 5.x polyfill for random_bytes() and random_int() from PHP 7
paypal/rest-api-sdk-php 1.12.0 PayPal's PHP SDK for REST APIs
phpseclib/phpseclib 2.0.6 PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.
psr/cache 1.0.1 Common interface for caching libraries
psr/http-message 1.0.1 Common interface for HTTP messages
psr/log 1.0.2 Common interface for logging libraries
rackspace/php-opencloud v1.16.0 PHP SDK for Rackspace/OpenStack APIs
ramsey/uuid 3.7.1 Formerly rhumsaa/uuid. A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).
ratchet/rfc6455 v0.2.3 RFC6455 WebSocket protocol handler
react/cache v0.4.1 Async caching.
react/dns v0.4.11 Async DNS resolver for ReactPHP
react/event-loop v0.4.3 Event loop abstraction layer that libraries can use for evented I/O.
react/promise v2.5.1 A lightweight implementation of CommonJS Promises/A for PHP
react/promise-timer v1.2.0 Trivial timeout implementation for Promises
react/socket v0.8.4 Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP
react/stream v0.7.3 Event-driven readable and writable streams for non-blocking I/O in ReactPHP
react/zmq v0.3.0 ZeroMQ bindings for React.
rize/uri-template 0.3.2 PHP URI Template (RFC 6570) supports both expansion & extraction
symfony/event-dispatcher v2.8.27 Symfony EventDispatcher Component
symfony/http-foundation v3.3.9 Symfony HttpFoundation Component
symfony/polyfill-mbstring v1.5.0 Symfony polyfill for the Mbstring extension
symfony/routing v3.3.9 Symfony Routing Component
tedivm/jshrink v1.2.0 Javascript Minifier built in PHP
twilio/sdk 5.15.0 A PHP wrapper for Twilio's API

I had to downgrade to evenement 2.1.0 because react/zmq won't intall wit evenement 3.0

@clarkk I think your problem has been resolved. React had updated one of their libs with an API break but Ratchet's constraints have since been updated to handle this. I copy/pasted your code with the latest version of Ratchet from a fresh install and it worked.

@RonaldoMoraes Can you elaborate on your problem?

@lmfmaier I opened reactphp/zmq#40 for your issue.

The "Argument 2 passed to ReactSocketServer::__construct() must implement interface ReactEventLoopLoopInterface, none given" error has been resolved by making changes to my source code:

Old:

$webSock = new React\Socket\Server($loop);
$webSock->listen(88, '0.0.0.0');

New:

$webSock = new React\Socket\Server('0.0.0.0:88',$loop);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hemant3105 picture Hemant3105  路  6Comments

Erseni picture Erseni  路  5Comments

DmitryPorotov picture DmitryPorotov  路  3Comments

grappetite-ali picture grappetite-ali  路  5Comments

lubatti picture lubatti  路  6Comments