Is it possible to implement Node.js in php file ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to implement Node.js in php file ?

Hello everyone ! I did a website using PHP, and I want to add a dynamic webchat using Node.js. I don't know if it is possible, I tried but nothing happens. If it is possible, please explain how. Really thank you if you have the solution !:)

13th Mar 2017, 10:07 PM
Volts
Volts - avatar
7 Answers
+ 1
Erf, this is a bit long, but I'll try ! Server: ===================== var http = require('http'); var fs = require('fs'); var server = http.createServer(function(req, res) { fs.readFile('./localhost/index.html', 'utf-8', function(error, content) { res.writeHead(200, {"Content-Type": "text/html"}); res.end(content); }); }); var io = require('socket.io').listen(server); io.sockets.on('connection', function (socket) { console.log('A client has join !'); }); server.listen(8080); =================== (I am forced to use localhost/index.php because my PHP is running on Apache server, I didin't found how to make it readable by Node.js) "Client": =================== <!DOCTYPE html> <html> <head> <?php include("head.php"); ?> <title></title> </head> <body> <p> <?php echo("Hello World"); ?> </p> <script src="socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost:8080'); </script> </body> </html> =================== (I did a copy of the socket.io folder)
14th Mar 2017, 5:59 PM
Volts
Volts - avatar
+ 1
I have two servers, the Node.js server(Running on port 8080) and the Apache server (Running on port 80)
14th Mar 2017, 6:15 PM
Volts
Volts - avatar
+ 1
Sorry, dont know. But maybe you'll look at php websoket chats, if you already use php
14th Mar 2017, 6:38 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 1
Interesting, I didn't know there were sockets with PHP !
14th Mar 2017, 8:00 PM
Volts
Volts - avatar
0
can u show me your code?
14th Mar 2017, 9:59 AM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
0
me too
14th Mar 2017, 8:03 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
0
search for "stream_socket_server()"
14th Mar 2017, 8:08 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar