Grupo Chat supports both WebSocket and AJAX-based real-time communication methods. If you’re on shared hosting or your server doesn’t support WebSockets, you can opt for the AJAX-based real-time approach. For WebSocket functionality, we utilize PHP Swoole for high-performance asynchronous communication.
How to Use WebSocket Instead of AJAX for Real-Time Communication ?
- Click on the Menu.
- Select Settings from the side navigation menu.
- Choose Realtime Settings
- In the “Realtime Mode” field, select your “WebSocket“
- Enter your Swoole WebSocket connection details in the form that appears
- Click the “Update” button.
For Your Reference :
- “WebSocket Host” refers to the server address (hostname or IP) where your WebSocket server is running.
- The WebSocket URL is the full address your application uses to connect to the WebSocket server. It includes the protocol (
ws://
orwss://
), the host, and the port (if needed). - The WebSocket Port is the specific port number your WebSocket server listens on for incoming connections. Ports 9501 and 9502 are commonly used by Swoole for WebSocket servers.
- The WebSocket Protocol determines whether the connection is secure or non-secure. (wss:// or ws://)
How to Install Swoole on Your Server ?
To begin, SSH into your server and execute the necessary commands directly from the terminal. Before starting, ensure that the following PHP extensions are installed and enabled on your server: sockets, openssl, and mbstring.
1) Update your system
sudo apt update && sudo apt upgrade -y
2) Install required dependencies
sudo apt install php-pear php-dev
3) Install Swoole via PECL
sudo pecl install swoole
4) Enable the Swoole PHP extension
5) Restart PHP, Apache, or Nginx services
6) Set up a reverse proxy using Apache or Nginx to handle the WebSocket URL.
In case your server is running Nginx, add the following configuration to enable access to your WebSocket at wss://grupo_domain/ws/.
location /ws/ {
proxy_pass http://127.0.0.1:9502;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
In case your server is running Apache, create a /ws/ folder and add an .htaccess file to configure it for WebSocket proxying
<IfModule mod_proxy.c>
<IfModule mod_proxy_wstunnel.c>
# Proxy WebSocket connections to the backend
ProxyPass "/" "ws://127.0.0.1:9502/"
ProxyPassReverse "/" "ws://127.0.0.1:9502/"
</IfModule>
</IfModule>
Make sure to:
- Replace 127.0.0.1 with your server’s IP address
- Change port
9502
to your preferred WebSocket port, if you’re using a different one
7) Restart PHP, Apache, or Nginx services
8) To check if it’s working properly,
Navigate to your Grupo folder in the terminal and execute php websocket.php to start the WebSocket server.
9) To run the WebSocket as a background process that will restart automatically in case of a crash or server restart, you can follow these steps:
— Install Supervisor
sudo apt-get install supervisor
— Create a Supervisor Configuration File for the WebSocket:
sudo nano /etc/supervisor/conf.d/websocket.conf
— Add the following content, modifying the path and user as needed. Make sure to replace “replace_with_your_grupo_path” with the full path to your grupo directory, and “user=www-data” with the correct user.
[program:websocket]
command=php /replace_with_your_grupo_path/websocket.php
autostart=true
autorestart=true
stderr_logfile=/var/log/websocket.err.log
stdout_logfile=/var/log/websocket.out.log
user=www-data
directory=/replace_with_your_grupo_path
— Update Supervisor and Start the WebSocket Process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start websocket
This will start the WebSocket process in the background and automatically restart it if it crashes or the server is restarted.