I got a connection between PHP and Node using Redis working fine, but I'm intrigued by doing it without any external libraries and services and just with sockets.
I'm not sure on how much performance will suffer without having Redis in the middle, but for simpler projects, this would be nice with lesser code.
I got most of the code from this answer in Using PHP with Socket.io but I had to change some of the vars. But it's not working as expected.
I tried both with and without https, since I will be using https, but I tried making a simpler example with http just to get it working.
- send.php creates and writes to a socket. I can confirm that it connects to node, since I get an error if node isn't running.
- socket.js doesn't return anything in the console.logs.
- socket-https.js returns connection from ::ffff:127.0.0.1 but isn't reading the socket.on('data'). What did I miss?
I think I misunderstand something in the var/require section in the http-version.
Can anyone spot why it's not working?
socket.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var listener = app.listen(8080, function(){
console.log('Listening on port ' + listener.address().port);
});
http.on("connection", function(socket) {
console.log("connection");
if(socket.remoteAddress == "::ffff:127.0.0.1") {
console.log("connection from " + socket.remoteAddress);
socket.on('data', function(buf) {
var js = JSON.parse(buf);
console.log("on data: " + js); // Not working
});
}
});
socket-https.js
var fs = require('fs');
var privateKey = fs.readFileSync('my-domain-com.key', 'utf8');
var certificate = fs.readFileSync('my-domain-com.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var app = require('express')();
var https = require('https').createServer(credentials, app);
var io = require('socket.io')(https);
https.listen(8080, function(){
console.log('Server online');
});
https.on("connection", function(socket) {
console.log("connection"); // Works!
if(socket.remoteAddress == "::ffff:127.0.0.1") {
console.log("connection from " + socket.remoteAddress); // Works!
socket.on('data', function(buf) {
var js = JSON.parse(buf);
console.log("on data: " + js); // Not working :(
});
}
});
send.php
sio_message("Message","Data");
function sio_message($message, $data) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, '127.0.0.1', 8080);
if(!$result) {
die('cannot connect '.socket_strerror(socket_last_error()).PHP_EOL);
} else {
echo "Connected<br>";
}
$bytes = socket_write($socket, json_encode(Array("msg" => $message, "data" => $data)));
echo " $bytes bytes written";
socket_close($socket);
}
console.logever fire? Is the problem thejson.parseerroring with your json data? Any errors in your logs? I'm just wondering if it's ever making it into that final message at all. I'd be interested in seeing the raw data that's sent. Maybe https is doing something with it compared to http? I ran the original code on anhttpserver so I'm wondering if that's the issue. Let me know what the raw data looks likeconsole.log(buf)and let me know if you see anything there :3 I don't think so - I'd check your php logs for errors too if you can.