using php with socket io

using php with socket io  using -'php,'

Is it possible to use 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 seems to be for node.js on the server side, so no hlep there.

asked Sep 9, 2015 by rajesh
0 votes

6 Answers

0 votes

It may be a little late for this question to be answered, but here is what I found.

I don't want to debate on the fact that nodes does that better than php or not, this is not the point.

The solution is : I haven't found any implementation of for PHP.

But there are some ways to implement WebSockets. There is this jQuery plugin allowing you to use Websockets while gracefully degrading for non-supporting browsers. On the PHP side, there is this class which seems to be the most widely used for PHP WS servers.

answered Sep 9, 2015 by rajesh
0 votes
answered Sep 9, 2015 by rajesh
0 votes

If you want to use together with php this may be your answer!

project website:

they are also on github: provides a 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 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

use ElephantIO\Client as Elephant;

$elephant = new Elephant('http://localhost:8000', '', 1, false, true, true);

    json_encode(array('name' => 'foo', 'args' => 'bar'))

echo 'tryin to send `bar` to the event `foo`';

socket io server

var io = require('').listen(8000);

io.sockets.on('connection', function (socket) {
  console.log('user connected!');

  socket.on('foo', function (data) {
    console.log('here we are in action event and data is: ' + data);
answered Sep 9, 2015 by rajesh
0 votes

How about this ? PHPSocketio ?? It is a php server side alternative. The event loop is based on pecl event extension. Though haven't tried it myself till now.

answered Sep 9, 2015 by rajesh
0 votes

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 (since you know, only works with node.js).

In case you still want to use, you can try this way: - find & get for client to use - work with Ratchet to simulate the way does on server

Hope this helps!

answered Sep 9, 2015 by rajesh
0 votes

Erm, why would you want to? Leave PHP on the backend and NodeJS/Sockets to do its non-blocking thing.

Here is something to get you started:

Personally I have express running with an endpoint that is listening expressly for interaction from PHP.

For example, if I have sent a user an email, I want to display a real-time notification to the user.

Want interaction from to php, well you can just do something like this:

var http = require('http'),
            host = WWW_HOST,
            clen = 'userid=' + userid,
            site = http.createClient(80, host),
            request = site.request("POST", "/modules/nodeim/includes/",  

request.write('userid=' + userid);      

Seriously, PHP is great for doing server side stuff and let it be with the connections it has no place in this domain now. Why do anything long-polling when you have websockets or flashsockets.

answered Sep 9, 2015 by rajesh