Is it possible to use Sockets.io on the client side and communicate with a PHP based application on the server? Does PHP even support such a 'long-lived connection' way of writing code?
All the sample code I find for socket.io seems to be for node.js on the server side, so no help there.
For 'long-lived connection' you mentioned, you can use Ratchet for PHP. It's a library built based on Stream Socket functions that PHP has supported since PHP 5.
For client side, you need to use WebSocket that HTML5 supported instead of Socket.io (since you know, socket.io only works with node.js).
In case you still want to use Socket.io, you can try this way: - find & get socket.io.js for client to use - work with Ratchet to simulate the way socket.io does on server
Hope this helps!
If you want to use socket.io together with php this may be your answer!
project website:
elephant.io
they are also on github:
https://github.com/wisembly/elephant.io
Elephant.io provides a socket.io client fully written in PHP that should be usable everywhere in your project.
It is a light and easy to use library that aims to bring some real-time functionality to a PHP application through socket.io and websockets for actions that could not be done in full javascript.
example from the project website (communicate with websocket server through php)
php server
socket io server
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
And here is the client.php or client.html sourcecode
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!
I haven't tried it yet, but you should be able to do this with ReactPHP and this socket component. Looks just like Node, but in PHP.
We are now in 2018 and hoola, there is a way to implement WS and WAMPServer on php. It 's Called Ratchet.
Look in this libraryes for php http://phptrends.com/category/70. Or use native from php http://www.php.net/manual/en/book.sockets.php .