Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php ratchet websocket SSL connect?

I have a ratchet chat server file

use Ratchet\Server\IoServer; use Ratchet\WebSocket\WsServer; use MyAppChat\Chat; require dirname(__DIR__) . '/vendor/autoload.php'; $server = IoServer::factory(     new WsServer(         new Chat()     )   , 26666 ); $server->run(); 

I using Websocket to connect with ws and it works fine

if ("WebSocket" in window) {     var ws = new WebSocket("ws://ratchet.mydomain.org:8888");     ws.onopen = function() {         // Web Socket is connected. You can send data by send() method.         ws.send("message to send");     };     ws.onmessage = function (evt) {          var received_msg = evt.data;     };     ws.onclose = function() {          // websocket is closed.      }; } else {   // the browser doesn't support WebSocket. } 

I want secure connection, so I try to connect with SSL but is not work.

if ("WebSocket" in window) {     var ws = new WebSocket("wss://ratchet.mydomain.org:8888");     ws.onopen = function() {         // Web Socket is connected. You can send data by send() method.         ws.send("message to send");     };     ws.onmessage = function (evt) {          var received_msg = evt.data;     };     ws.onclose = function() {          // websocket is closed.      }; } else {   // the browser doesn't support WebSocket. } 

My question is how to connect websocket with SSL connection

Any idea?

like image 709
Johnny Nguyen Avatar asked Jun 07 '13 08:06

Johnny Nguyen


2 Answers

If you are using Apache web server (2.4 or above), enable these modules in httpd.conf file :

  1. mod_proxy.so
  2. mod_proxy_wstunnel.so

Add this setting to your httpd.conf file

ProxyPass /wss2/ ws://ratchet.mydomain.org:8888/ 

Use this URL in your JavaScript call when you want a WSS connection:

var ws = new WebSocket("wss://ratchet.mydomain.org/wss2/NNN"); 

Restart Apache web server and make sure that your Ratchet worker (web socket connection) is open before applying the settings (telnet hostname port).

like image 81
webcoder Avatar answered Sep 21 '22 14:09

webcoder


A few days ago I was looking for the answer of this question and I found this in the Github Ratchet issues: https://github.com/ratchetphp/Ratchet/issues/489

The last answer, answered by heidji, says this:

I only added this comment for newbies like me who need a quick instruction how to implement SSL: Via the ReactPHP docs you only need to construct the SecureServer mentioned in such manner:
$webSock = new React\Socket\Server('0.0.0.0:8443', $loop);
$webSock = new React\Socket\SecureServer($webSock, $loop, ['local_cert' => '/etc/ssl/key.pem', 'allow_self_signed' => true, 'verify_peer' => false]);
and then inject into the IoServer as mentioned by cboden above

So it seems that now there is a way to implement a secure websocket server with Ratchet without needing an HTTPS proxy.

Here you have the SecureServer class documentation: https://github.com/reactphp/socket#secureserver

like image 30
Jordi Avatar answered Sep 22 '22 14:09

Jordi