3

Hi I'm trying to develop a chat system using socket.io, express.io and node.js everything is going smoothly, and I have been following the documentation from them.

The problem is as soon as I'm trying to integrate the design to the skeleton app that I have developed, it wont load correctly. It only gives me a popup notifying me to either download or open the PHP file.

here is my index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
  res.sendfile('message.php');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log('message: ' + msg);
  });
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});
http.listen(3000, function(){
  console.log('listening on *:3000');
});

Am I doing something wrong here?

Roger
  • 7,535
  • 5
  • 41
  • 63
Kim Oliveros
  • 711
  • 1
  • 7
  • 28
  • 1
    I have tried searching for it and no good I cant seem to find a solution... – Kim Oliveros Jul 31 '14 at 08:34
  • how are you handling this client side ? and you really should consider the use of an express template engine like ejs or node... – MaK Jul 31 '14 at 08:38
  • I think it does exactly what you have told it with your code. What you have done is sending a PHP file to the client. What are you trying to do? Do you want the PHP file to execute and send the result of _that_ execution to client? – sampathsris Jul 31 '14 at 09:53
  • 1
    @Krumia Yes I need to execute the php file because the whole interface is there... if i put instead message.html instead of message.php it will work.. – Kim Oliveros Jul 31 '14 at 10:04
  • @kastormania this are running under node.. sorry but what exactly do you mean? – Kim Oliveros Jul 31 '14 at 10:05
  • yeah of course it's running under nodejs, your server is doing exactly what you're asking it to do : sending a php file back to the client, but nodejs server won't execute your php code. with nodejs and express you can use an engine template like [ejs](http://embeddedjs.com/getting_started.html) to render your "design" – MaK Jul 31 '14 at 10:14
  • @kastormania but its still on html format i believe? ejs is the same as jade? – Kim Oliveros Jul 31 '14 at 10:54
  • 1
    ejs and jade are both template engines, but each one has its own syntax – MaK Jul 31 '14 at 10:55
  • 1
    I see so how can node interpret my php file? sorry if I'm asking stupid questions because I'm new to websocket and node so basically my knowledge here is so little – Kim Oliveros Jul 31 '14 at 10:58
  • you cand use the [php-node](https://www.npmjs.org/package/php-node) package to execute your php code – MaK Jul 31 '14 at 11:15
  • ok I'll research that thanks for the input @kastormania – Kim Oliveros Jul 31 '14 at 11:29

1 Answers1

2

socket.io sendfile will just send the contents of the file to the client. It does not matter if you give a PHP script or an image file to sendfile. It does the same.

If you want to execute a PHP script, you can do one of the following:

  • Run a separate web server that interprets PHP, and then connect to that server from Node.js, as described in this answer.
  • Run the PHP interpreter with child_process, then send the standard output of that process to client.
Community
  • 1
  • 1
sampathsris
  • 21,564
  • 12
  • 71
  • 98