node.js running alongside Apache PHP?
Asked Answered
I

5

31

I am trying to get my head round node.js...

I am very happy with my LAMP set up as it currently fulfils my requirements. Although I want to add some real-time features into my PHP app. Such as showing all users currently logged into my site and possible chat features.

I don't want to replace my PHP backend, but I do want scalable real-time solutions.

1. Can I throw node.js into the mix to serve my needs without rebuilding the whole application server-side script?

2. How best could node.js serve my 'chat' and 'currently logged in' features?

Great to hear your views!

W.

Ieshaieso answered 9/1, 2011 at 20:39 Comment(0)
D
23

I suggest you use Socket.io along side node.js. Install and download the libs from http://socket.io/. You can run it along side your Apache server no problems.

First create a node server:

var http = require('http')
  , url = require('url')
  , fs = require('fs')
  , io = require('../')//path to your socket.io lib
  , sys = require(process.binding('natives').util ? 'util' : 'sys')
  , server;

server = http.createServer(function(req, res){
  var path = url.parse(req.url).pathname;
}),

server.listen(8084);//This could be almost any port number

Second, run your server from the command line using:

node /path/to/your/server.js

Third, connect to the socket using client side js:

var socket = new io.Socket(null, {port: 8084, rememberTransport: false});
socket.connect();

You will have to have include the socket.io lib client side aswell.

Send data from client side to the node server using:

socket.send({data:data});

Your server.js should also have functions for processing requests:

io.on('connection', function(client){
//action when client connets

 client.on('message', function(message){
    //action when client sends msg
  });

  client.on('disconnect', function(){
    //action when client disconnects
  });
});

There are two main ways to send data from the server to the client:

client.send({ data: data});//sends it back to the client making the request

and

client.broadcast({  data: data});//sends it too every client connected to the server
Duvalier answered 16/2, 2011 at 11:7 Comment(4)
hi @Duvalier thanks for this great answer. I'm still a little confused about the connecting to Apache part. Would you mind adding some comments/code to your answer to make the Apache part more clear?Planet
What about the same origin policy? You wont be able to make calls to a different port using socket io, no?Lapham
@KitCarrau when we have a php extension file eg -> index.php then we need apache to run it and at the same time if we have to load our socket.io <script> on client side -> index.php to activate our node.js and socket.io feature it doesn't works.One reason is port where we cannot use apache and nodejs port no. sameBoth
and if we want to use different port for those two separate servers apache and nodejs then how can we run together our php and nodejs ??Both
E
2

I suspect the chat as well as the logged in listing would work via Ajax.

The chat part would be pretty easy to program in Node.js, use one of the mysql modules for Node to connect to your existing database and query login information and such and then do all the actual chatting via Node.js, I recommend you to check out Socket.io since it makes Browser/Node.js communcation really trivial, this should allow you to focus on the actual chat logic.

Also, you could take a look at the "official" chat demo of Node.js, for some inspiration.

As far as the currently online part goes, this is never easy to implement since all you can do is to display something along the lines of "5 users active in the last X minutes".

Of course you could easily add some Ajax that queries the chat server and display the userlist from that on the homepage.

Or you completely crazy and establish a Socket.io connection for every visitor and monitor it this way, although it's questionable whether this is worth the effort.

Ebonieebonite answered 9/1, 2011 at 21:26 Comment(3)
Wouldn't he be forced to run the node.js server on another port as port 80 is already in use by Apache or have it bind to another IP? He would then run into cross-domain issues if he ever wanted to communicate with the chat server and his current web app. I might be wrong, but I believe this was essentially his question.Gentile
Depends, he should be able to pass it through without having the URL change: arguments.callee.info/2010/04/20/…Ebonieebonite
Interesting find. One of the comments on that post suggests doing the opposite. I.e. Having node.js do the proxying to Apache instead of the other way around. If you use Apache as a proxy for node.js, Apache will spawn a new process every request and forward it to node.js.Gentile
V
1

What about using a socket file just like pedro did with ngnx ? http://nodetuts.com/tutorials/25-nginx-and-nodejs.html

Ventose answered 15/8, 2011 at 17:13 Comment(0)
J
0

You can run php from node js using node-php: https://github.com/mkschreder/siteboot_php

Jumna answered 26/1, 2014 at 21:55 Comment(0)
M
0

I'm running a wss (secure websocket) server alongside my LAMP setup.

Node.js can easily run alongside any other web server (apache) you want. In @KitCarrau example, he lets node run on port 8084 - that's where it's running and listening to, not 80 or 443 etc (those are usually taken by apache anyway). But you can still use the same port to also serve http/https (in my case just stating some conf and general info that the service is up).

Starting from the console isn't the best way (remotely, node stops when closing the console). I recommend taking a look at Running node as service

Easy to track log in realtime (log with console.log("hello"); in your application) with:

tail -f /var/.../SocketServer.log

Sample script (node-server.conf):

author ....    
description "node.js server"    
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

# Max open files are @ 1024 by default. Bit few.
limit nofile 32768 32768

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="/root"

    exec node /var/.../SocketServer.js >> /var/www/node/.../SocketServer.log 2>&1
end script

post-start script
   # Optionally put a script here that will notifiy you node has (re)started
   # /root/bin/hoptoad.sh "node.js has started!"
   echo "\n*********\nServer started\n$(date)\n*********" >> /var/.../SocketServer.log

end script
Munson answered 11/11, 2014 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.