Real Time Operations with PHP and Socket.io
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 https://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 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/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.
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></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) {
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('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 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
<?php
require __DIR__ . '/vendor/autoload.php';
use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;
$client = new Client(new Version2X('http://IP_ADRES:5000'));
$client->initialize();
$client->emit('new-post', [
'title' => 'phpExample.Net',
'content' => 'test',
'date' => date('Y-m-d'),
'id' => 2
]);
$client->close();
This is the situation what we are dealing with. See you :)
Comments
Popular Articles
Popular Tags
- #directive
- #Function
- #Json
- #Class
- #SASS
- #Form
- #Redirect
- #API
- #Special Characters
- #Special
- #Random
- #Generating
- #
- #Text
- #Ajax
- #URL
- #Encrypting
- #React
- #Show
- #timeAgo
- #PSR-4
- #Hide
- #DDOS Protection
- #DDOS
- #cURL
- #Logic For Displaying Posts
- #Error
- #Key
- #General Error
- #1364 Field
- #Abbreviation
- #Blade
- #Version
- #QR
- #QR Code Generating
- #Array
- #Arrays with Key Values to String Statement
- #Short Tag
- #Activate
- #Real Time
- #Socket.io
- #301
- #Custom Directives
- #Iframe Detection
- #Date
- #Characters
- #Insert
- #Autoloader
- #Composer
- #Reading
Visitor
13/06/2022 07:25
I'm trying to emit an event from a PHP controller with elephant io by using version2x but I get a version1x error.
Error Message:
UnexpectedValueException: The server returned an unexpected value. Expected "HTTP/1.1 101", had "HTTP/1.1 400" in vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php:266
PHPEXAMPLE
14/07/2022 14:39
hello @visitor, first of all, do you still have the problem? If you share your js code, I will try to help you.
Sanghavi
18/08/2022 11:15
thanks the code worked
Visitor
28/12/2022 09:20
thanks