1.Create a folder in xampp/htdocs/phpsocket
ex: C:\xampp\htdocs\phpsocket
Install node
2.Open terminal or cmd and type npm to test
3.After that change directory to phpsocket
4.Now in cmd type npm install express socketio
(You should be inside phpsocket folder to install express and socketio)
5.Create two files index.php and server.js in folder
server.js
==========
var app= require('express')();
var http=require('http').Server(app);
var io=require('socket.io')(http);
io.sockets.on('connection', function(client) {
console.log("New client");
client.on('message', function(data){
console.log(data);
});
});
http.listen(8080, function(){
console.log("Listening at :8080")
});
var http=require('http').Server(app);
var io=require('socket.io')(http);
io.sockets.on('connection', function(client) {
console.log("New client");
client.on('message', function(data){
console.log(data);
});
});
http.listen(8080, function(){
console.log("Listening at :8080")
});
index.php
========
<?php
echo "test";
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script>
var socket= io.connect('http://localhost:8080');
socket.emit('message','cool');
</script>
</head>
<body>
</body>
</html>
Also you can create package.json file
=====================
{
"name": "phpsocket",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"express": "^4.14.0",
"socketio": "^1.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC"
}
Then at last run node server.js and open php file in browser.
(In command prompt or cmd or terminal you should be inside the folder
ex: C:\xampp\htdocs\phpsocket
)
ex: C:\xampp\htdocs\phpsocket
)
check cmd or terminal for result.
Extra Tips:(Run node server from php)
===============================
Create a bat file name it as run_node.bat
put content as
---------------
node index.js
===============================
Create a bat file name it as run_node.bat
put content as
---------------
node index.js
Create a another bat file name it as stop_node.bat
put content as
----------------
taskkill /F /IM "node.exe"
Create a php file admin.php
-------------------------------------
<?php
$cmd='C:\xampp\htdocs\FRB_new\test_box\rooms\run_bat.bat';
$stop_bat='C:\xampp\htdocs\FRB_new\test_box\rooms\stop_bat.bat';
if(isset($_POST['on'])) {
if (!($handle_run = popen ('start /B '.$cmd.' >nul 2>&1', 'r') ))
{
echo "Error";
}
else
{
echo "Running";
}
}
if(isset($_POST['off'])) {
//system('cmd /c C:\xampp\htdocs\FRB_new\test_box\rooms\stop_bat.bat');
if (!($handle_stop = system('cmd /c C:\xampp\htdocs\FRB_new\test_box\rooms\stop_bat.bat') ))
{
echo "Error";
}
else
{
echo "Stopped";
}
}
?>
<form action="bat_test.php" method="POST" >
<input type="submit" value="Start the node" name="on" />
<input type="submit" value="Kill the node" name="off" />
</form>
------------------------------------------------------
Remember, keep all these in same folder in chat.
----------------
taskkill /F /IM "node.exe"
Create a php file admin.php
-------------------------------------
<?php
$cmd='C:\xampp\htdocs\FRB_new\test_box\rooms\run_bat.bat';
$stop_bat='C:\xampp\htdocs\FRB_new\test_box\rooms\stop_bat.bat';
if(isset($_POST['on'])) {
if (!($handle_run = popen ('start /B '.$cmd.' >nul 2>&1', 'r') ))
{
echo "Error";
}
else
{
echo "Running";
}
}
if(isset($_POST['off'])) {
//system('cmd /c C:\xampp\htdocs\FRB_new\test_box\rooms\stop_bat.bat');
if (!($handle_stop = system('cmd /c C:\xampp\htdocs\FRB_new\test_box\rooms\stop_bat.bat') ))
{
echo "Error";
}
else
{
echo "Stopped";
}
}
?>
<form action="bat_test.php" method="POST" >
<input type="submit" value="Start the node" name="on" />
<input type="submit" value="Kill the node" name="off" />
</form>
------------------------------------------------------
Remember, keep all these in same folder in chat.