Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't connect more than one websockets simultaneously #52

Closed
nazreinkaram opened this issue Aug 22, 2017 · 7 comments
Closed

Can't connect more than one websockets simultaneously #52

nazreinkaram opened this issue Aug 22, 2017 · 7 comments
Labels

Comments

@nazreinkaram
Copy link

nazreinkaram commented Aug 22, 2017

Unable to connect to two or more WebSocket services simultaneously,

The second request goes in waiting and instantly connect when first connection closes

here is what I did

<?php

    #    PHP 7.1.8 (cli) (built: Aug  1 2017 20:56:32) ( NTS MSVC14 (Visual C++ 2015) x64 )
    #    Copyright (c) 1997-2017 The PHP Group
    #    Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    
    
    require __DIR__ . '/vendor/autoload.php';


    $websocketURLs[] = ['url' => 'wss://echo.websocket.org', 'service' => 'websocket'];

    $websocketURLs[] = ['url' => 'ws://echo.socketo.me:9000', 'service' => 'socketo'];


    foreach( $websocketURLs as $v ){
    
        connect_to_websocket ( $v['url'], $v['service'] );

    }

    
    function connect_to_websocket ( &$url, &$service){

        $responseCount = 0;

        echo "\nOK... Will connect to $url at $service\n\n";

   
        \Ratchet\Client\connect( $url )->then(function ($connection) use ( &$url, &$service, &$responseCount ) {

            echo "Connected to $service\n";

            $connection->send( "Hello" );

            $connection->on('message', function($message) use ($connection, &$url, &$service, &$responseCount) {

                $responseCount++;
            
                $message = (string) $message;

                echo "$service - $message - Response no. $responseCount - Will disconnect at response no. 10\n";

                if( $responseCount == 10)

                    $connection->close();
            
                $connection->send( $message );

            });

            $connection->on('close', function($code = null, $reason = null) use ( &$url, &$service) {

                echo "Websocket Connection to $service is closed ({$code} - {$reason}). Reconnecting...\n";

                connect_to_websocket ( $url, $service );
            
            });

        }, function ($e) {
        
        exit("Could not connect to websocket: {$e->getMessage()}. Exiting...\n");
    
    });

}

This only connects to one resource at a time and second connection goes to waiting, as the first connection closed, second (waiting) connection connects instantly.

Also, there is unlimited timeout, how to provide or edit timeout? If remote server is not running at the time of creating connection (calling connect_to_websocket () in above code) then the script freezes, it doesn't connect to server when server comes online, which needs force closing/canceling the script

PHP CLI gives this

image

@nazreinkaram nazreinkaram changed the title Can't connect more than one websocket simultaneously Can't connect more than one websockets simultaneously Aug 22, 2017
@cboden
Copy link
Member

cboden commented Aug 22, 2017

Can you post all of your code so I can reproduce please? What's given isn't enough to go on.

@nazreinkaram
Copy link
Author

Updated original post as requested

@cboden
Copy link
Member

cboden commented Aug 23, 2017

The connect function is an abstraction to hide the event loop, which is fine for one connection, but as you've found doesn't work for multiple as it creates an event loop per call. You need to inject a single event loop into each connection.

You will need to use the Connector class instead, as seen in the second example of the README to achieve multiple connections.

@nazreinkaram
Copy link
Author

nazreinkaram commented Aug 24, 2017

This one worked for multiple connections perfectly.

But the problem of freezing still exists

If remote server is not running at the time of creating connection (calling connect_to_websocket () in below code) then the script freezes, it doesn't connect to server when server comes online, which needs force closing/canceling the script

And where to edit or pass timeout?

<?php

    require __DIR__ . '/vendor/autoload.php';


    $loop = React\EventLoop\Factory::create();
    
    $connector = new Ratchet\Client\Connector($loop);

    
    $websocketURLs[] = ['url' => 'wss://echo.websocket.org', 'service' => 'websocket'];

    $websocketURLs[] = ['url' => 'ws://echo.socketo.me:9000', 'service' => 'socketo'];


    foreach( $websocketURLs as $v ){
    
        connect_to_websocket ( $v['url'], $v['service'], $connector );

    }

    
    function connect_to_websocket( &$url, &$service, &$connector){

        $responseCount = 0;

        echo "\nOK... Will connect to $url at $service\n\n";
        
        $connector( $url, [], [] )->then(function ($connection) use ( &$url, &$service, &$connector, &$responseCount ) {

            echo "Connected to $service";
            
            echo "\n============================\n";

            $connection->send( "Hello" );

            $connection->on('message', function($message) use ($connection, &$url, &$service, &$responseCount) {

                $responseCount++;
            
                $message = (string) $message;

                echo "$service - $message - Response no. $responseCount - Will disconnect at response no. 10";
                echo "\n---------------------------------------------------------------------------\n";

                if( $responseCount == 10)

                    $connection->close();
            
                $connection->send( $message );

            });

            $connection->on('close', function($code = null, $reason = null) use ( &$url, &$service, &$connector) {

                echo "Websocket Connection to $service is closed ({$code} - {$reason}). Reconnecting...\n";

                connect_to_websocket( $url, $service, $connector );
            
            });

        }, function ($e) {
        
        exit("Could not connect to websocket: {$e->getMessage()}. Exiting...\n");
    
    });

}

$loop->run();

image

@jaggedsoft
Copy link

I am also having the same problem, I will attempt the fix mentioned above

require __DIR__.'/vendor/autoload.php';
// The console should show both ETHBTC and BNBBTC if two websocket connections are established
\Ratchet\Client\connect('wss://stream.binance.com:9443/ws/ethbtc@depth')->then(function($conn) {
	$conn->on('message', function($msg) use($conn) {
		echo "{$msg}\n";
	});
	$conn->on('close', function($code = null, $reason = null) {
		echo "WebSocket Connection closed ({$code} - {$reason})\n";
	});
}, function($e) {
	echo "Could not connect: {$e->getMessage()}\n";
});

\Ratchet\Client\connect('wss://stream.binance.com:9443/ws/bnbbtc@depth')->then(function($conn) {
	$conn->on('message', function($msg) use($conn) {
		echo "{$msg}\n";
	});
	$conn->on('close', function($code = null, $reason = null) {
		echo "WebSocket Connection closed ({$code} - {$reason})\n";
	});
}, function($e) {
	echo "Could not connect: {$e->getMessage()}\n";
});

@jaggedsoft
Copy link

Using the loop above fixed it for me. Thank you
Is it possible to use more than one loop?

@kelunik
Copy link

kelunik commented Jan 26, 2018

No, everything needs to run on the same event loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants