I have an existing web application on the standard PHP/Apache platform. Now, what I want is to add chat functionality, and I want it to be real-time over a websocket, and to that extend I've studied socket.io on nodejs a little bit. So in addition to Apache running the big PHP app, I would have nodejs with socket.io running chat.
But what I don't really understand, is how would I recognise my users in the code for nodejs chat? For one, Apache and nodejs won't be able to run on the same port, meaning I'll run chat on port 8080
for example, in which case I lose the user's cookies, which then means I now have to ask them to log in once more on this nodejs-powered port if they want to use chat? Seems ridiculous, but I don't know which way to go about it.
I cannot port my entire code onto nodejs, of course. So ideally I would want Apache and nodejs to coexist. Or I am just completely misunderstanding how chat is supposed to work in web apps.
Any tips appreciated.
You can run your Apache with PHP on e.g. port 3001 and your Node app on port 3002, and have nginx configured as a reverse proxy to make them both available on port 80, for example your PHP app in the root
/
directory and your Node app in the/chat
directory, with nginx config like this:With SSL it would be slightly more complicated but not that much:
Your PHP and Node apps could even run on different servers - just use their addresses in the nginx config.
See this answer and its comments for more details: