Using Symfony router with ratchet wamp server

1,057 views
Skip to first unread message

xiki...@gmail.com

unread,
Jul 19, 2014, 1:13:24 PM7/19/14
to ratch...@googlegroups.com
Hello guys,

I've been wrapping my head around trting to setup my websockets wamp server with url routing.

What i'm trying to acheive is having multuple services under the same http server (something like 127.0.0.1:8080/foo for 1 service and  127.0.0.1:8080/bar for another)

I'm searching onlie for examples and I cannot find one, I've tried searching the Ratchet code to see if I can found where the routing should be done and nothing.

Can anyone help me on this? Is this even possible?

Thanks

William G.

unread,
Jul 20, 2014, 2:21:26 PM7/20/14
to ratch...@googlegroups.com, xiki...@gmail.com
You could use HAProxy or nginx to route for you and have two different services.

developm...@gmail.com

unread,
Jul 26, 2014, 6:38:14 AM7/26/14
to ratch...@googlegroups.com, xiki...@gmail.com
I need the similar thing, so in case you find some solution, please just post here.

I will do the same if I find some working solution.

Thanks,
Milos

xiki...@gmail.com

unread,
Jul 26, 2014, 10:51:50 AM7/26/14
to ratch...@googlegroups.com, xiki...@gmail.com
Hello guys,

I finally figured it out after taking a second look at things again.

Turns out it's pretty easy. You just have to use the Symfony componets for routing, something like this


    $collection = new RouteCollection;
    $collection->add('foo', new Route('/foo', array(
        '_controller'    => new Ratchet\WebSocket\WsServer(
                                new Ratchet\Wamp\WampServer(
                                    new SocketServer\FooPusher
                                )
                            ),
        'allowedOrigins' => 'dev.foo.com'
    )));
    $collection->add('bar', new Route('/bar', array(
        '_controller'    => new Ratchet\WebSocket\WsServer(
                                new Ratchet\Wamp\WampServer(
                                     new SocketServer\BarPusher
                                )
                            ),
        'allowedOrigins' => '*'
    )));

then declare a new router and wrap it in the httpserver class.

 $router = new Ratchet\Http\Router(
                    new UrlMatcher($collection, 
                                   new RequestContext()
                    )
              );

    $webServer = new Ratchet\Server\IoServer(
                    new Ratchet\Http\HttpServer(
                        $router
                    ),
                    $webSock
    );


I think this is pretty simple, the only problem I had was calling the service with wamp2.0 protocol I don't think ratchet supports it.

If someone is falling to do this, feel free to post it here.

I might be able to help

Cheers
Francisco

cboden

unread,
Aug 10, 2014, 10:14:36 AM8/10/14
to ratch...@googlegroups.com, xiki...@gmail.com
Thanks for posting your findings Francisco! My apologies for the lack of documentation on the matter. 

In addition to Francisco's suggestion check out the App class. It wraps a lot of the Ratchet and Symfony Routing boilerplate to make adding separate routes/endpoints easier. 

wellens....@gmail.com

unread,
Mar 16, 2015, 7:11:50 AM3/16/15
to ratch...@googlegroups.com, xiki...@gmail.com
Hi,

Could someone please provide an example on how to use the App class to do some routing?
Does the App class need to be wrapped in the server or the other way around?

cboden

unread,
Mar 20, 2015, 11:43:59 AM3/20/15
to ratch...@googlegroups.com, xiki...@gmail.com, wellens....@gmail.com
Check out the example in the readme. It shows adding a couples routes to an App instance. App creates all the IoServer, HttpServer, WsServer, and Route classes for you; you just have to provide the URI and class to App for each route. 
Reply all
Reply to author
Forward
0 new messages