How To Create Secure Server With Ratchet Websocket Php
In this tutorial I will show you all required steps in order to implement a websocket server using Ratchet library. Ratchet is PHP library which provide necessary tools for developers to implement real time applications. Using ratchet you can create a websocket server which will handle all concurent connections. Then, from server side you can send all data you need using ZMQ to update the user screen with data provided.
How to implement SSL with Ratchet PHP
In order to be able to use https with Ratchet you will have to add the relative paths to cert and its key. From last update you can do it very easy without any proxy or other hacks. In push-server.php you should have the following code in order to work secured connection.
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
With the following line of code we will create a secured server and this is the place where we should add relative path of cert.
$webSock = new React\Socket\SecureServer( $webSock, $loop, array( 'local_cert' => '/home/domain/ssl/certs/abc.crt', 'local_pk' => '/home/domain/ssl/keys/abc.key', 'verify_peer' => false, 'verify_peer_name' => false ) );
With all of these already in our file, the single thing we need to do is to initiate the server and run. So, this is the code:
$webServer = new Ratchet\Server\IoServer( new Ratchet\Http\HttpServer( new Ratchet\Session\SessionProvider( new Ratchet\WebSocket\WsServer( new Ratchet\Wamp\WampServer( $pusher ) ),$session ) ), $webSock ); $loop->run();
As you can see in the above code I am using the SessionProvider because I need user id and other things like this. If you don't need these details you can remove those lines of code. Besides of this code you will have to require all the namespaces Ratchet is using. Let me know if this solution worked for you.
this documentation is not complete. $session was never defined, you also never define $loop.
Can you please paste full code here ??? Thanks in advance