Socket = io php example

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 socket.io, you can handle this situation

Topic List

  • Setting up node.js on Centos server
  • Setting Socket.io
  • Socket.io 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 //rpm.nodesource.com/setup_10.x | 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

2-Setting Socket.io

We will set the socket.io 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 Socket.io

npm install --save socket.io

3-Socket.io Settings

Here is the settings for socket.io . 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['socket.io'][server];

// this works when connected to the socket
io.on['connection', function[socket]{
    
    console.log['connecting..'];

    // 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
server.listen[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  //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 //IP_ADRES:5000/socket.io/socket.io.js file.

4-Listening socket on client side

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

Then we connect the our socket address.

var socket = io['//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] {
    console.log[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['//IP_ADRESI:5000', {
    secure: true
}]

5-Sending data from PHP to socket

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

composer require wisembly/elephant.io

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

Chủ Đề