+ 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 !:)
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)
+ 1
I have two servers, the Node.js server(Running on port 8080) and the Apache server (Running on port 80)
+ 1
Sorry, dont know. But maybe you'll look at php websoket chats, if you already use php
+ 1
Interesting, I didn't know there were sockets with PHP !
0
can u show me your code?
0
me too
0
search for "stream_socket_server()"