I've coded a website using JS, HTML, and PHP. I have notifications that currently run in PHP with a check to the MYSQL database every 10 seconds, as well as a messaging script that also checks the database for changes every 10 seconds. I'd like to use Node.JS and websockets (socket.io) on top of my PHP pages to handle the notification and messaging systems, rather than checking the database every 10 seconds, while still have the actual page generated and loaded by PHP.
My website is secured with SSL (thank you LetsEncrypt!), and is running a linux Apache server on AWS.
I've been able to get the basic example running from the socket.io page, but I want it to: a. Integrate with my PHP code AND b. Not show the port number in the url
I have $_SESSION variables when a user logs in and I need to access them to pass them on to node with each message, such as the User-ID. Ajax requests give me a cross-domain-error, and if they don't, they don't pass on the sessid cookie with the request to a PHP script.
I've tried setting proxies and reverse proxies in my httpd.conf as well, to poor results. In some cases nothing happened, and in others it redirected all requests to Node, meaning my Apache php pages didn't even get processed.
I've also looked at this question, which seems to cover what I am talking about, except the SSL part. The thing is, Tim's code hasen't worked for me - If I access the file without the port in the url, the page loads but doesn't connect to Node and returns "socket.io.js 404 (Not Found)" (probably due to proxies in httpd conf), and if I access it with the port, the browser returns "unexpectedly closed the connection". If I try specifying the port on HTTP, I just get a blank page. If I don't, I get "Forbidden" in the browser.
My question is - is there a way to set up what I'd like to do with Node and Apache (and if so, how)? I'd rather not switch to nginx, so if that's not an option, are there Node alternatives that'll achieve what I want?
I'm absolutely new to coding with Apache and Node.js, but pretty proficient with JS, PHP and HTML, so let me know if I can provide any more info!
UPDATE: Maybe some code snippets will help
httpd.conf
NameVirtualHost *:443
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName domain
ServerAlias www.domain.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/domain.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/domain.com/chain.pem
DocumentRoot /var/www/html
<Directory />
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /chat>
ProxyPass https://127.0.0.1:3003
ProxyPassReverse https://127.0.0.1:3003
</Location>
</VirtualHost>
<IfModule mod_proxy.c>
ProxyRequests On
<Proxy *>
Order deny,allow
Deny from all
Allow from .domain.com
</Proxy>
</IfModule>
Server Side JS
var io = require('socket.io').listen(3003);
io.sockets.on('connection', function(socket) {
socket.on('message_to_server', function(data) {
io.sockets.emit("message_to_client",{ message: data["message"] });
});
});
Client Side JS
var socket = io.connect('https://localhost:3003', {secure: true});
window.baseURL = 'https://domain.com/';
$.ajax({
url: "https://domain.com/chat/session.php",
crossDomain: true,
xhrFields: { withCredentials: true }
}).done(function( data ) {
console.log(data);
console.log(data.type);
socket.emit('set nickname', data.uid);
});
And the session.php is really simple
<?php
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: domain.com");
session_start();
$name = $_SESSION['d1'];
$surname = $_SESSION['d2'];
$pic = $_SESSION['d3'];
$uid = $_SESSION['d4'];
$email = $_SESSION['d5'];
$uniname = $_SESSION['d6'];
$uniid = $_SESSION['d7'];
$output = json_encode(array("type" => 'success', "uid" => $uid, "ppic" => $pic, "name" => $name, "surname" => $surname, "uniid" => $uniid, "email" => $email, "uniname" => $uniname), JSON_FORCE_OBJECT);
exit($output);
?>
Lastly, there is an HTML file that glues all the client side stuff together.
Now, the way I see it, I can do two things. Either request the session data by AJAX, but then I run into the whole problem with Cross Domain origins, etc, and the big problem above.
The other way, is convert the index.html to an index.php and bypass the whole AJAX request. I still run into the problems as above, since PHP needs Apache and Node needs a port and each of them want a unique port :P.
Have I gone insane?