This is a websockets chat example on php and js. It's made for training purposes, it's not production-ready, nor will it ever be. And it's architecture leaves much to be desired. It shows what awesome thing is websockets protocol... even on php!
How to install:
- Swoole installs as php extension, see the link: https://www.swoole.co.uk/docs/get-started/installation
- Chat server launches as cli application: 'php websocket-server.php'. Chat.html, css and main.js must be placed somewhere in www/htdocs of apache/nginx/whatever webserver you use.
- Run websocket-server.php, use your browser to navigate to chat.html and have fun!
- php file actually runs behind another ip address (127.0.0.1:9503 by default, but can be changed in Application.php), so you need to configure webserver (apache in my case) to proxy every ws request from this IP to IP of your webserver. Part of my apache virtualhost for example:
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:9503 [P,L]
Example of chat.html window:
Reference:
https://www.swoole.co.uk/docs/
https://github.com/swoole/swoole-docs/
