Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to PHP using Apache's mod_proxy_wstunnel without using 3rd Party APIs

I am new to WebSocket world and even after a week, I still could not find how to use it with PHP without using any 3rd Party Library. Or not sure if it is even possible.

I have 3 questions,

1) Do we must need to use some 3rd Party APIs or Libraries like Ratchet, PHP-Push-WebSocket or PHP WebSocket to enable PHP to communicate over WebSocket protocol?

2) If your answer to above question is yes then what is the advantage/purpose of using Apache's mod_proxy_wstunnel?

3) If I use any PHP WebSocket 3rd Party Library, then do I still need to enable and use Apache's mod_proxy_wstunnel? If No, then again what is the purpose of Apache's mod_proxy_wstunnel?

I went through this Using go-websocket behind Apache mod_proxy_wstunnel. In this question, the OP has indicated some go-websocket but unfortunately the link is giving 404 Error and hence I cannot understand if the user has used any 3rd Party API.

Any help would be highly appreciated. Thanks in advance.

like image 508
Airy Avatar asked Jul 18 '17 09:07

Airy


2 Answers

PHP is not created for WS. Of course you can do this, but it is full of while (true) and fsock_open [*]. And you have to have access to the shell to run the WS-server with php-cgi. (usually)

Choose a language that supports threads / asynchronous communication. Now on the top is Node.js. In addition, better use dedicated libraries to support older browsers, etc. If you are thinking about alternatives to ajax then you can use socket.io. If you want to create one-to-all communication (eg chat, broadcast messaging), go one step further and use one of the Bayeux implementations, for example: Faye.

The mod_proxy_wstunnel extension adds an extra layer to the WS server. Depending on the configuration, it can support DDoS, queue, load-balancing, local port swapping, and https support. But better use this for nginx.

So, for test you don't need mod_proxy_wstunnel, and on production, you should add an extra layer of security, eg NGINX ws tunelling.

[*] Sorry, I made some mistakes. PHP has native support for WebSockets. That content is going to set you negatively to WS in PHP.

Websockets are events, and PHP is poorly managed with it.

like image 199
bato3 Avatar answered Oct 19 '22 10:10

bato3


Just to elaborate a bit on why Apache + PHP is unsuitable for WebSocket support...

1) Do we need to use some 3rd Party APIs or Libraries like Ratchet, PHP-Push-WebSocket or PHP WebSocket to enable PHP to communicate over WebSocket protocol?

No, you can just open a local socket and accept requests using low-level Socket API (example), or you can even upgrade an existing request to WebSocket. But there is a problem - read on...

2) If your answer to above question is yes then what is the advantage/purpose of using Apache's mod_proxy_wstunnel?

The advantage of mod_proxy_wstunnel is that it allows users to establish WebSocket connections with your server using the server's HTTP(S) port (usually 80 or 443) instead of connecting on a custom port (e.g. 9000). This does not eliminate step 1 though, as it simply proxies WebSocket connections from Apache to the custom port.

3) If I use any PHP WebSocket 3rd Party Library, then do I still need to enable and use Apache's mod_proxy_wstunnel?

The problem with Apache / PHP setup is that PHP code always runs in scope of a specific HTTP request. This is a problem because Apache is not designed for long-running requests. Apache can support a very small amount of active requests (usually 150 or so), which is barely enough to handle regular (short-lived) traffic on an medium-size site. In addition, by default max_execution_time is set to 5 minutes, which means any request (and any WebSocket connection inside of it) will be killed after that.

If No, then again what is the purpose of Apache's mod_proxy_wstunnel?

mod_proxy_wstunnel handles persistent connections without tying up a request thread. So it allows one Apache instance to handle hundreds of thousands of WebSocket connections. But again, it's just a tunnel, it can't "invoke" PHP, as it can only proxy a WebSocket connection to some other application.

If you have access to the server machine, you can run a standalone PHP script, which would handle WebSocket connections as a daemon outside Apache (e.g. via systemd), and optionally use mod_proxy_wstunnel to proxy requests to it.

But by itself, Apache + PHP architecture is unsuitable for production-grade WebSocket support.

like image 33
rustyx Avatar answered Oct 19 '22 09:10

rustyx