Real Time Operations with PHP and

In your PHP projects, you may need to display some parts of your code according to real time. In such circumstances, by using PHP with, you can handle this situation

Topic List

  • Setting up node.js on Centos server
  • Setting
  • Settings
  • Listening socket on client side
  • Sending data from PHP to socket

1-Setting up node.js on Centos server

To do this you just need to run this commands

yum install -y gcc-c++ make curl -sL | sudo -E bash -
yum install nodejs

To check the setting up is right, look at the version of npm and node.js

node -v
npm -v


We will set the with the ngm that package manager of node.js . To generate package.json file let run this command

npm init -y

Now we can set the

npm install --save Settings

Here is the settings for . Let's create app.js file and let's write this codes inside

// we are installing nodejs http server
const server = require('http').createServer();
const io = require('')(server);

// this works when connected to the socket
io.on('connection', function(socket){

    // we are listening to the new-post event, we will send data from the backend here
    socket.on('new-post', function(data){
        // If there is data, we send it to the client.
        io.emit('posts', data);

    // This works when the socket connection ends
    socket.on('disconnect', function(){
        console.log('someone came and went');

// we listen on port 5000

We listen with on, we sent with emit to client. Now launch the server with nodejs. But let's do this with pm2 package. So we dont have to reboot the program, let working in the background, Firstly let set the pm2 package.

npm install pm2 -g
pm2 start app.js

Now we can listen the socket as  http://IP_ADRES:5000. If there is nothing shows up when you enter it is normal, because it just requires socket connection. To be sure everything is working correct you can check the http://IP_ADRES:5000/ file.

4-Listening socket on client side

This part is the easiest part, First of all we need to include the js library of to page.

<script src=""></script>

Then we connect the our socket address.

var socket = io('http://IP_ADRESI:5000');

// we are listening to the posts event on the socket, if it comes, we will print it and look at the console.
socket.on('posts', function (data) {

//If we wanted to send more to the socket's posts event by the client, we would still use emit.
socket.emit('posts', {
    'id': 5,
    'title': 'phpExample.Net',
    'content': 'this is test',
    'date': '2021-03-01 20:00:00'

If you are using SSL, then you can open a safe socket connection in this way.

var socket = io('https://IP_ADRESI:5000', {
    secure: true

5-Sending data from PHP to socket

To do this we are going to use a package name of . To set the package on our project

composer require wisembly/

When we send a data to socket we use in this way


require __DIR__ . '/vendor/autoload.php';

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;

$client = new Client(new Version2X('http://IP_ADRES:5000'));
$client->emit('new-post', [
    'title' => 'phpExample.Net',
    'content' => 'test',
    'date' => date('Y-m-d'),
    'id' => 2

This is the situation what we are dealing with. See you :)