Ratchet: Websocket server with IPv6/Dual Stack?

Created on 20 Jul 2014  路  9Comments  路  Source: ratchetphp/Ratchet

Is there any way I can make my Websocket server listen on IPv4 and IPv6?

Is this something that PHP does not support (since I can't find any documentation on how to make socket_bind use IPv6 at all...) or could I somehow hack this in anyway?

Any help is greatly appreciated ^.^

question

Most helpful comment

Try this:

<?php
    require dirname(__DIR__) . '/vendor/autoload.php';

    $port = 8080;
    $ip6Addr = '???';

    $loop = React\EventLoop\Factory::create();
    $ip4 = new React\Socket\Server($loop);
    $ip6 = new React\Socket\Server($loop);

    $ip4->listen($port, '0.0.0.0');
    $ip6->listen($port, $ip6Addr);

    $wsStack = new Ratchet\Http\HttpServer(
        new Ratchet\WebSocket\WsServer(
            new MyApp
        )
    );

    $ip4App = new Ratchet\Server\IoServer($wsStack, $ip4, $loop);
    $ip6App = new Ratchet\Server\IoServer($wsStack, $ip6, $loop);

    $loop->run();

I'm not familiar with IPv6 and I don't know if you can listen to an IP4 and IP6 connection on the same port, but this code should get you started.

All 9 comments

PHP supports IPv6, however I am not aware of how to bind ratchet to an IPv6 address. Would like some input from @cboden on this.

Regards,
Jadenn

Support for IPv6 was added a while ago to React and should just work (Ratchet runs on top of react/socket). Simply supply an IPv6 address.

Any way to get React to listen on both protocols at the same time?

I was also more interested in @DakotaShaw's question. My server is reachable with both and I would like the WebSocket Server to listen on both addresses instead of only one.

So, is there any way to make this happen? Currently I'm only listening on IPv4 and I took care that the clients get an IPv4 address without looking it up themselves (since if they would just use the DNS they'd try to connect with an IPv6 address)

Can't really comment on this code wise as I haven't tried it yet. But my guess is that you can make two different binds (one IPv4 and one IPv6) and both use the same callback/method to handle new connections. (Just speculating here.)

Infrastructure wise I'm running everything behind Varnish. So everything connects to varnish (both normal HTTP requests and websockets), varnish then creates a new connection (it's acting as a proxy here) between it and the websocket server over IPv4. So even if the connection reaches varnish as IPv6 internally it works over IPv4. Effectively exposing the service over both IPv4 and IPv6.

@cboden Not sure how this works internally, but is it possible now to treat two different connections (IPv4 and IPv6) effectively as one. So broadcasts from IPv4 would also reach clients on IPv6?

Try this:

<?php
    require dirname(__DIR__) . '/vendor/autoload.php';

    $port = 8080;
    $ip6Addr = '???';

    $loop = React\EventLoop\Factory::create();
    $ip4 = new React\Socket\Server($loop);
    $ip6 = new React\Socket\Server($loop);

    $ip4->listen($port, '0.0.0.0');
    $ip6->listen($port, $ip6Addr);

    $wsStack = new Ratchet\Http\HttpServer(
        new Ratchet\WebSocket\WsServer(
            new MyApp
        )
    );

    $ip4App = new Ratchet\Server\IoServer($wsStack, $ip4, $loop);
    $ip6App = new Ratchet\Server\IoServer($wsStack, $ip6, $loop);

    $loop->run();

I'm not familiar with IPv6 and I don't know if you can listen to an IP4 and IP6 connection on the same port, but this code should get you started.

:+1: and yes you can. Since they are both completely separated you can listed on the same port for both. You can even have one program listen on port 80 on IPv4 and another on port 80 on IPv6.

I finally got around to trying this out and while the code provided works fine under Windows it does not seem to work with Ubuntu 12.04.

I doubt that this is a configuration problem since Apache, SSH, Tomcat, ... can all listen on both protocols just fine. Any idea why this isn't working with Linux?
I have PHP 5.5.16 and just updated the composer dependencies before trying this.

sweet

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebinator picture sebinator  路  9Comments

elovin picture elovin  路  3Comments

maliknaik16 picture maliknaik16  路  8Comments

grappetite-ali picture grappetite-ali  路  5Comments

Lugia101101 picture Lugia101101  路  7Comments