Skip to content

Socket Plugin

Daniel Neto edited this page Mar 10, 2023 · 19 revisions

WebSockets enable the server and client to send messages to each other at any time, after a connection is established, without an explicit request by one or the other. This is in contrast to HTTP, which is traditionally associated with the challenge-response principle — where to get data one has to explicitly request it. In more technical terms, WebSockets enable a full-duplex connection between the client and the server. WebSockets allow for a higher amount of efficiency compared to REST because they do not require the HTTP request/response overhead for each message sent and received.

From now on we will avoid Ajax requests and we will use more WebSockets, so some ajax features will be deprecated and we will only support WebSockets, so is strongly recommended you start using it.

This plugin is based on Ratchet WebSockets for PHP Ratchet is a loosely coupled PHP library that provides developers with tools to create bidirectional applications in real-time between clients and servers via WebSockets.

Check List

Firewall

Make sure your firewall has the socket port opened, by default we use port 2053, but you can change it on the plugin´s property

SSL Certificate

  1. Make sure you have a valid SSL certificate
  2. Proxy certificates are not tested
  3. We always use the letsencrypt SSL, it works pretty good for the socket

PHP Version

Something that I noticed, there are some strange behaviors if your PHP is older than PHP v7.4. Therefore, it is highly recommended that you use PHP 7.4 or later

You may want to use this tutorial to upgrade your PHP

For PHP7.4 make sure you install this

sudo apt-get install nano curl apache2 php7.4 libapache2-mod-php7.4 php7.4-mysql php7.4-curl php7.4-gd php7.4-intl php7.4-xml -y

Update also the PHP command line

sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set phar /usr/bin/phar7.4
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4

Run the WebSocket server

You can run the Socket server any time with the command,

sudo php /var/www/html/AVideo/plugin/YPTSocket/server.php

But if you close the terminal or interrupt the command your server will stop, so you can use the nohup command to avoid interrupt the server process

Nohup is short for No Hangups. It's not a command that you run by itself. Nohup is a supplemental command that tells the Linux system not to stop another command once it has started. That means it'll keep running until it's done, even if the user that started it logs out.

Your command may look like this:

sudo nohup php /var/www/html/AVideo/plugin/YPTSocket/server.php &

Run the WebSocket server on reboot

Ubuntu 16

Edit the /etc/rc.local file. The /etc/rc.local file on Ubuntu and Debian systems is used to execute commands at system startup.

sudo nano /etc/rc.local

Add this line on the rc.local file

nohup php /var/www/html/AVideo/plugin/YPTSocket/server.php &

Ubuntu 18 or greater

There is no /etc/rc.local file on Ubuntu 18 or greater, so you can use the crontab to start your server on reboot

Edit crontab as root:

sudo crontab -eu root

Add this line on crontab

@reboot sleep 60;nohup php /var/www/html/AVideo/plugin/YPTSocket/server.php &

Sleep 60 is required, you must include a sleep time on reboot to give time to the webserver to load first, then launch the Socket script.

Production server

If you will have over 1k users at the same time you may need to follow this http://socketo.me/docs/deploy#ulimit

Those are the commands that work for me

export PKG_CONFIG_PATH=/usr/lib/pkgconfig
sudo apt-get install libevent-dev php-pear php-dev make pkg-config

Edit php.ini nano /etc/php/7.4/apache2/php.ini and add extension=event.so

Troubleshooting

Restart Socket

In most cases, a socket issue can be resolved by simply restarting the socket. This is because sockets can sometimes become disconnected or unresponsive due to various reasons such as network connectivity issues, server overload, or bugs in the software.

When you restart the socket, it essentially resets the connection and starts it anew, which can resolve any issues that were preventing it from functioning properly. This is why restarting the socket is often the first step in troubleshooting any socket-related issues.

However, if the issue persists even after restarting the socket, then you may need to look into other factors such as the firewall settings, proxy configurations, or SSL certificate problems to determine the root cause of the issue.

To restart the socket, follow these steps:

  1. Stop the socket or any app on the socket port by killing it – Run this command:
sudo php /var/www/html/AVideo/plugin/YPTSocket/stopServer.php
  1. Run the socket again – Run this command:
sudo php /var/www/html/AVideo/plugin/YPTSocket/server.php

Restart does not work

If you're having trouble with a socket connection, you can perform some tests on your own server to diagnose the issue. Follow these steps to get started:

Step 1: Open Two Separate SSH Terminals

To perform the tests, you'll need to have two separate SSH terminals open. This will allow you to run multiple commands simultaneously and check the output of each.

Step 2: Check for Common Issues

Before running the tests, check for common issues that can cause socket connections to fail. These can include port blocking by a firewall, proxy issues, or SSL certificate problems.

Step 3: Stop Any Running Sockets or Apps on the Port

To start the tests, make sure your socket is not running and that there are no other apps or processes using the same port. You can stop any running sockets or apps by running the following command in Terminal 1:

sudo php /var/www/html/AVideo/plugin/YPTSocket/stopServer.php

Step 4: Start the Socket and Check for Errors

Next, start the socket again and check the output for any errors or clues that may help diagnose the issue. Run the following command in Terminal 1:

sudo php /var/www/html/AVideo/plugin/YPTSocket/server.php

Step 5: Test the Socket Connection

Finally, run a test on your own server to see if you can connect to the socket. Keep Terminal 1 open and go to another terminal to run the tests. Run the following command in Terminal 2:

sudo php /var/www/html/AVideo/plugin/YPTSocket/test.php

If the test is successful, you should see a message indicating that the connection was successful. If not, review the output from the previous steps to help diagnose the issue.

Remember to troubleshoot and address any issues found during the testing process before attempting to use the socket connection again.

Here are the outputs from a working socket server

Stop
root@youphptube2:~# php /var/www/html/demo.avideo.com/plugin/YPTSocket/stopServer.php 
[2022-01-26 12:16:22] AVideoLog::DEBUG: getStart language: got from config  SCRIPT_NAME: /var/www/html/demo.avideo.com/plugin/YPTSocket/stopServer.php
Searching for port: 2053
Server is already runing on port 2053 Killing, PID 24374
root@youphptube2:~# 
Start
root@youphptube2:~# php /var/www/html/demo.avideo.com/plugin/YPTSocket/server.php     
[2022-01-26 12:17:39] AVideoLog::DEBUG: getStart language: got from config  SCRIPT_NAME: /var/www/html/demo.avideo.com/plugin/YPTSocket/server.php
[2022-01-26 17:17:39] AVideoLog::DEBUG: Starting Socket server at port 2053 SCRIPT_NAME: /var/www/html/demo.avideo.com/plugin/YPTSocket/server.php
Starting AVideo Socket server version 2.8 on port 2053
Your socket server uses a secure connection
Parameter [local_cert]: /etc/letsencrypt/live/demo.avideo.com/fullchain.pem 
Parameter [local_pk]: /etc/letsencrypt/live/demo.avideo.com/privkey.pem 
Parameter [allow_self_signed]: 1 
Parameter [verify_peer]:  
Parameter [verify_peer_name]:  
Parameter [security_level]: 0 
2022-01-26 17:17:39 Construct
Test (Terminal 2)
root@youphptube2:~# php /var/www/html/demo.avideo.com/plugin/YPTSocket/test.php  
[2022-01-26 12:18:39] AVideoLog::DEBUG: getStart language: got from config  SCRIPT_NAME: /var/www/html/demo.avideo.com/plugin/YPTSocket/test.php
[2022-01-26 17:18:39] ** Starting socket test **
[2022-01-26 17:18:39] Testing connection with [0]: ws://localhost:2053 on 2022/01/26 17:18:39
[2022-01-26 17:18:39] Testing connection with [1]: wss://localhost:2053 on 2022/01/26 17:18:39
[2022-01-26 17:18:39] Testing connection with [2]: ws://127.0.0.1:2053 on 2022/01/26 17:18:39
[2022-01-26 17:18:39] Testing connection with [3]: wss://127.0.0.1:2053 on 2022/01/26 17:18:39
[2022-01-26 17:18:39] Testing connection with [4]: ws://demo.avideo.com:2053 on 2022/01/26 17:18:39
[2022-01-26 17:18:39] Testing connection with [5]: wss://demo.avideo.com:2053 on 2022/01/26 17:18:39
[2022-01-26 17:18:39] ERROR on get connect response [Connection closed before handshake]
[2022-01-26 17:18:39]    SECURE CONNECTION    CONNECTION FAIL wss://localhost:2053 Connection to tls://localhost:2053 failed during TLS handshake: Peer certificate CN=demo.avideo.com' did not match expected CN=localhost'
[2022-01-26 17:18:39] ERROR on get connect response [Connection closed before handshake]
[2022-01-26 17:18:39]    SECURE CONNECTION    CONNECTION FAIL wss://127.0.0.1:2053 Connection to tls://127.0.0.1:2053 failed during TLS handshake: Peer certificate CN=demo.avideo.com' did not match expected CN=127.0.0.1'
[2022-01-26 17:18:39] ERROR on get connect response [Connection closed before handshake]
[2022-01-26 17:18:39]    SECURE CONNECTION    CONNECTION SUCCESS wss://demo.avideo.com:2053 
Test Response (Terminal 1)
2022-01-26 17:18:39 New connection (602) TESTING-61f1826fa881a TESTING-terminal TESTING
2022-01-26 17:18:39 Test detected and received from (602) 
*** SUCCESS TEST CONNECION wss://demo.avideo.com:2053 on 2022/01/26 17:18:39 ***

What to expect on the test

What really matters to us is a secure successful connection

[2022-01-26 17:18:39]    SECURE CONNECTION    CONNECTION SUCCESS wss://demo.avideo.com:2053 

and

*** SUCCESS TEST CONNECION wss://demo.avideo.com:2053 on 2022/01/26 17:18:39 ***
Clone this wiki locally