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 ?

  1. Click on the Menu.
  2. Select Settings from the side navigation menu.
  3. Choose Realtime Settings
  4. In the “Realtime Mode” field, select your “WebSocket
  5. Enter your Swoole WebSocket connection details in the form that appears
  6. 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:// or wss://), 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;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_cache_bypass $http_upgrade;

    proxy_read_timeout 3600s;
    proxy_send_timeout 3600s;
    send_timeout 3600s;
    keepalive_timeout 3600s;
}

If you’re using Apache, edit the virtual host file and add the configuration inside the <VirtualHost> block:

    <IfModule mod_proxy.c>
        <IfModule mod_proxy_wstunnel.c>
            ProxyPass "/ws/" "ws://127.0.0.1:9502/"
            ProxyPassReverse "/ws/" "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
  • Before you can use ProxyPass and mod_proxy_wstunnel, ensure the required Apache modules are enabled.

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:

— Increase File Descriptor Limit (Optional but Recommended)

grep -qxF '* soft nofile 65535' /etc/security/limits.conf || echo '* soft nofile 65535' | sudo tee -a /etc/security/limits.conf
grep -qxF '* hard nofile 65535' /etc/security/limits.conf || echo '* hard nofile 65535' | sudo tee -a /etc/security/limits.conf

To temporarily set the file descriptor limit to 65535 for your current shell session, run: ulimit -n 65535

— Create a systemd Service File for the WebSocket:

sudo nano /etc/systemd/system/websocket.service

— Add the following content. Replace “replace_with_your_grupo_path” with the full path to your grupo directory, and adjust “User=www-data” as needed.

[Unit]
Description=PHP WebSocket Server
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/replace_with_your_grupo_path
ExecStart=php /replace_with_your_grupo_path/websocket.php
Restart=always
RestartSec=3
LimitNOFILE=65535
StandardOutput=append:/var/log/websocket.out.log
StandardError=append:/var/log/websocket.err.log

[Install]
WantedBy=multi-user.target

— Reload systemd and Enable the WebSocket Service

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable websocket.service
sudo systemctl start websocket.service

This will start the WebSocket process in the background and automatically restart it if it crashes or the server is restarted.