Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an idea in mind and would like to know if that's the way to go for my end application.

Think of my application as a social networking system in which I want to implement chat functionality. For that, I'd like to push data from the server to the client. I have heard I could use Node.js for that.

In the meanwhile, I want a 'regular' system for posting status updates and such, for which I'd like to use PHP and an apache server.

The only way I can think of is having Node.js and apache running parallel. But is that the way to go here? I'd think there would be a somewhat neater solution for this.

share|improve this question
    
Combine how? You can have node.js and apache running in parallel, and there's a variety of ways to pass data between them. What exactly are you trying to do? – Yannis May 21 '13 at 15:59
    
@YannisRizos sorry, in parallel, yes. – Camil Staps May 21 '13 at 16:24
    
up vote 10 down vote accepted

Running several web server processes in parallel is not a problem at all, people do it all the time.

However, since only one process can ever listen on any given port, they can't both run on port 80 directly, so you'll have to dispatch somehow. An easy solution is to run the Node.js application on a custom port (anything above 1024 should be fair game, but do a bit of research first, and make it configurable), then set up a reverse proxy in apache to pass it through on port 80 (or 443 if you need SSL) for the URLs that should go to Node.

Reverse proxying from Node into apache might also be possible, and if the Node application handles the most performance-critical calls, it may or may not be a better solution.

A thirds solution is to serve them both on different ports and do the reverse proxying in a separate process, or even a separate machine. The advantage of this is that splitting the setup over more application servers (e.g. dedicated boxes for the Node and PHP parts) later is relatively trivial (but then, it isn't much harder for the other two solutions either).

The nice part about having Apache on the public-facing side is that you can use its various mature features such as HTTP authentication, rewriting, SSL, virtual hosts, static file serving, etc., so the Node part can focus on the essentials. No need to reinvent the wheel there.

share|improve this answer
    
Actually anything above 1024 is fair game -- or at least out of the potential range of IANA reserved ports. – Wyatt Barnett Nov 8 '13 at 18:47

There's no technical reason you couldn't do this. I did something very similar for a simple game I wrote to play with Node. The pages were served by Apache and written in PHP, but then I had an HTML5 canvas and I initialized a socket to my Node server. Both Apache and Node can run happily side by side on the same machine, just different ports.

share|improve this answer

You could use node.js as the server:

you can set up a server in node, adding the following modules:

  • express: static html server
  • ws: websocket support
  • sphp: PHP support

You can make a simple and fast webserver, with websockets binding to port 80 and using PHP as server side scripting language. You can almost use the example server in sphp, as is.

I should mention that I'm the author of sphp. There are other options for PHP integration into node.

You install npm (Node package manager) to your system and then install the node modules you need:
In project directory

npm install express ws sphp 

then you copy the example.js and modify it to needs.

See examples her: https://github.com/paragi/sphp
For the basics see also: https://github.com/paragi/was

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.