Skip to content

Instantly share code, notes, and snippets.

@Eishfaq
Last active July 11, 2019 07:00
Show Gist options
  • Select an option

  • Save Eishfaq/b068a58755e8433ad0bd3209bd3c2ffe to your computer and use it in GitHub Desktop.

Select an option

Save Eishfaq/b068a58755e8433ad0bd3209bd3c2ffe to your computer and use it in GitHub Desktop.
Socket Programming ( Prodigyview)
#Installation
#The easy way to install the application is with composer. Start by running the require command in composer
composer require prodigyview/prodigyview
#sender script
use prodigyview\network\Socket;
use prodigyview\system\Security;
Security::init();
$socket = new Socket($ip, $port, array('connect' => true)); //Opening socket connection of the target server
$message = Security::encrypt(json_encode($data)); // encryption of the data you want to pass
$response = $socket->send($message); //Send the data to the target server
$socket->close(); //Close the connection
// Note: Here $ip = Your targer server's IP and $port = Your target server's port
#receiver script
use \prodigyview\system\Security;
use \prodigyview\network\Socket;
Security::init();
echo "Listening from the port started.........";
$server->startServer('', function($message) { // Here the message received
if($message != null){
$data = json_decode(Security::decrypt($message), true);
if(!empty($data) || $data != 'null' || $data != null){
echo "Data Found \n";
echo json_encode($data)."\n"; // Printing the data as JSON view just to easy read
}
}
else{
echo "No Data \n";
}
}, 'closure');
#The way to run worker process all the time
#This is a very important part to make the worker running all the time. Cause this worker is listening to the port and get the data.
#You server may reboot and the service can be stopped.
#You can use nohup to make the process running but the best option is to use Supervisor.
#INSTALL SUPERVISOR: Its easy and simple.
#For CentOS
yum install supervisor
#After installing open the file on your editor vi/nano/vim
vim /etc/supervisord.conf
#Then edit the conf file like the following
[program:<YOUR PROGRAM NAME>]
command=php /path/to/your-app/your-worker.php
process_name=%(program_name)s_%(process_num)02d
numprocs=8
priority=999
autostart=true
autorestart=true
startsecs=1
startretries=3
user=apache
redirect_stderr=true
stdout_logfile=/path/to/log/worker.log
#Start the supervisor
service supervisord start
supervisorctl reload
#Check your process status
supervisorctl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment