I know the struggle man! But I recently had it pretty much working with Workerman.
If you have not stumbled upon this php framework then you better check this out!
Well, Workerman is an asynchronous event driven PHP framework for easily building fast, scalable network applications. (I just copied and pasted that from their website hahahah http://www.workerman.net/en/)
The easy way to explain this is that when it comes web socket programming all you really need to have is to have 2 files in your server or local server (wherever you are working at).
server.php (source code which will respond to all the client's request)
client.php/client.html (source code which will do the requesting stuffs)
So basically, you right the code first on you server.php and start the server. Normally, as I am using windows which adds more of the struggle, I run the server through this command --> php server.php start
Well if you are using xampp. Here's one way to do it. Go to wherever you want to put your files. In our case, we're going to the put the files in
C:/xampp/htdocs/websocket/server.php
C:/xampp/htdocs/websocket/client.php or client.html
Assuming that you already have those files in your local server. Open your Git Bash or Command Line or Terminal or whichever you are using and download the php libraries here.
https://github.com/walkor/Workerman
https://github.com/walkor/phpsocket.io
I usually download it via composer and just autoload those files in my php scripts.
And also check this one. This is really important! You need this javascript libary in order for you client.php or client.html to communicate with the server.php when you run it.
https://github.com/walkor/phpsocket.io/tree/master/examples/chat/public/socket.io-client
I just copy and pasted that socket.io-client folder on the same level as my server.php and my client.php
Here is the server.php sourcecode
<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use PHPSocketIO\SocketIO;
// listen port 2021 for socket.io client
$io = new SocketIO(2021);
$io->on('connection', function($socket)use($io){
$socket->on('send message', function($msg)use($io){
$io->emit('new message', $msg);
});
});
Worker::runAll();
And here is the client.php or client.html sourcecode
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="chat-messages" style="overflow-y: scroll; height: 100px; "></div>
<input type="text" class="message">
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="socket.io-client/socket.io.js"></script>
<script>
var socket = io.connect("ws://127.0.0.1:2021");
$('.message').on('change', function(){
socket.emit('send message', $(this).val());
$(this).val('');
});
socket.on('new message', function(data){
$('#chat-messages').append('<p>' + data +'</p>');
});
</script>
</html>
Once again, open your command line or git bash or terminal where you put your server.php file. So in our case, that is C:/xampp/htdocs/websocket/ and typed in php server.php start and press enter.
Then go to you browser and type http://localhost/websocket/client.php to visit your site. Then just type anything to that textbox and you will see a basic php websocket on the go!
You just need to remember. In web socket programming, it just needs a server and a client. Run the server code first and the open the client code. And there you have it! Hope this helps!