I am trying to create a real time Symfony app using Ratchet but I dont know where shoud I put my WampServerInterface and my server script (in a symfony service or just a class somewhere) and how should I call it from my appController
2 Answers
1
votes
1
votes
First of all you would need to run ratchet server from command line.
You may chose to use symfony CLI for that as that would be the easiest way to get you started. I have not tested any of the following code, but something like the following would do.
<?php
namespace MyOrg\MyBundle\Command
{
use
// Symcony CLI
Symfony\Component\Console\Input\InputArgument,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console\Output\OutputInterface,
Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand,
// Ratchet classes are used with full paths in execute()
// Your ratchet app class (e.g. https://github.com/cboden/Ratchet-examples/blob/master/src/Ratchet/Website/ChatRoom.php)
MyOrg\MyBundle\MyRatchetAppClass;
class RatchetServerCommand extends ContainerAwareCommand
{
protected function configure(){
$this
->setName('myorg:ratchet')
->setDescription('Start ratchet server');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$loop = \React\EventLoop\Factory::create();
$app = new MyRatchetAppClass();
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new \React\Socket\Server($loop);
$webSock->listen(88, 'YOURSERVER.COM');
$webServer = new \Ratchet\Server\IoServer(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
new \Ratchet\Wamp\WampServer(
$app
)
)
),
$webSock
);
$loop->run();
}
}
}
Then start the server using symfony cli:
php app/console myorg:ratchet
At the end of this, you would have a ratchet server running on port 88.
After that, use a websocket library to connect and test. I am using [autobahnjs] in my example below:
ab.connect(
// The WebSocket URI of the WAMP server
'ws://yourserver.com:88',
// The onconnect handler
function (session) {
alert('Connected');
},
// The onhangup handler
function (code, reason, detail) {
alert('unable to connect...');
}
);