Using PHP with

04/10/2020 00:00:02

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 help there.

Answer #1 (98 Votes)

11/18/2011 13:25:05

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.


Answer #2 (70 Votes)

11/18/2012 04:56:33

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);
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.