I'd like to use Socket.io and Node.js to do push notifications. The end goal is to do something similar to what Stackoverflow does with their menting to notify people of new ments.
My site is in PHP and runs on Apache on an EC2 instance. I've heard that Apache doesn't handle concurrency well so I'm interested in using Node to handle the hopefully large # of simultaneous, persistent connections. I guess there are two solutions:
- Put Node on same instance as Apache and proxy the two servers
- Put Node on a separate instance
In either case, I don't know how the connection between these two servers looks programmatically. For example, while WebSockets/Node handles the sending/receiving of messages, I need to store these messages in my MySQL database and that would require some PHP code, yes/no? Also, how is the message received on my PHP page? Via a $.post
to a url like this, http://mysite/receiver.php
that would be resolved by my Apache server?
I'd be happy to see either ments or code to help me understand this better.
I'd like to use Socket.io and Node.js to do push notifications. The end goal is to do something similar to what Stackoverflow does with their menting to notify people of new ments.
My site is in PHP and runs on Apache on an EC2 instance. I've heard that Apache doesn't handle concurrency well so I'm interested in using Node to handle the hopefully large # of simultaneous, persistent connections. I guess there are two solutions:
- Put Node on same instance as Apache and proxy the two servers
- Put Node on a separate instance
In either case, I don't know how the connection between these two servers looks programmatically. For example, while WebSockets/Node handles the sending/receiving of messages, I need to store these messages in my MySQL database and that would require some PHP code, yes/no? Also, how is the message received on my PHP page? Via a $.post
to a url like this, http://mysite/receiver.php
that would be resolved by my Apache server?
I'd be happy to see either ments or code to help me understand this better.
Share Improve this question edited Jul 24, 2012 at 15:34 tim peterson asked Jul 24, 2012 at 15:29 tim petersontim peterson 24.3k63 gold badges185 silver badges303 bronze badges 5- 1 My experience with node.js is admittedly limited but as far as I understand, apples and oranges. A node.js server should not need to municate with an apache one — there are node.js libraries to handle MySQL connections though, so you can probably do whatever you need to in server-side javascript via node.js. As long as you are confident with JS you should be fine with no PHP involvement. – Mahn Commented Jul 24, 2012 at 15:48
- Also, but again as far as I understand, you should be fine running both node.js and apache on a single instance; I think you can just have a specific port handled by node.js and the usual 80/4430 via apache (someone with more node.js hands on experience may want to correct me on this if it is otherwise though) – Mahn Commented Jul 24, 2012 at 15:52
- @Mahn thanks, can you explain where I specify the port number? Is that part of the URL or is that PHP.ini or Node config code? And I think the Node is port 3000? – tim peterson Commented Jul 24, 2012 at 16:02
- Have a look at @GeoPhoenix answer since it seems he already covered it :) – Mahn Commented Jul 24, 2012 at 16:43
- @timpeterson Were you able to find some code snippet to implement your task? I am searching for the exact problem. – Akash Saikia Commented Apr 30, 2014 at 15:29
3 Answers
Reset to default 4- You can run different servers at different ports.
- In order for the servers to municate in a way you can use Sessions/MySQL/flat files/Cache/json/xml...
- You can store messages wherever you want there is no limitation on that, MySQL module for node.js exists, memcache module for node.js exists and many more, PHP code is not required, its optional to query the database.
- The messages on your PHP page are fetched from your database and rendered, if node.js stores stuff in mysql server you can fetch them with PHP or temporarily store to an array and push messages back to client while you store into database for new users to have "current" content.
- Avoid using ajax since you can stream data to client via websockets in realtime use when needed.
-- Edited
Some additional information on How to use vhosts alongside node-http-proxy? link provided from Timothy Strimple at ments , thanks
Some MySQL drivers can be found in this question What MySQL drivers are available for node.js?
About configuring ports:
-On apache you need to manually edit httpd.conf to define default port
-On node.js things are simpler and you can define a port in code
Using node.js as a simple web server
var connect = require('connect');
connect.createServer(
// .. code
).listen(PORT);
hope it helped
I need to store these messages in my MySQL database and that would require some PHP code, yes/no?
No. Node allows you to write javavscript on the server and you can do pretty much anything with it that you would do with PHP, including connect to a MySQL database.
You can run both Node and Apache on the same server and use NGINX to proxy between the two, or you could use Node to proxy to your Apache. But don't use Apache to proxy to Node as you'd be hurting performance.
Here are some links that will hopefully help:
- Express web framework
- node-mysql
- NGINX proxy_pass - run nginx on port 80, run apache and node on localhost with different ports, use nginx to route between them.
You could also use sockets as a direct means of munication between the two using node.js's net module and php's socket module. Here is an example.
Simple node js server listening for a connection
var net = require('net');
var listenport = 3000; // Should be the same as in the php script
// Set up server
var Server = net.createServer(function(Sock) {
console.log('Client Connected.');
Sock.on('data', function(data) {
console.log('Data received: ' + data);
dataobj = JSON.parse(data);
console.log('Item ID: ' + dataobj.itemid);
// and so on
});
Sock.on('end', function() {
console.log('Client Disconnected.');
});
Sock.pipe(Sock);
});
Server.listen(listenport, function() {
console.log('Listening on port ' + listenport);
});
Simple PHP connecting to the node instance
<?php
error_reporting(E_ALL);
$port = 3000; // Port the node app listens to
$address = '127.0.0.1'; // IP the node app is on
// Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
// Connect to node app
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
}
// Data we want to send
$data = array('itemid' => '1234567', 'steamid' => '769591951959', 'otherinfo' => 'hi there');
// Prepares to transmit it
$encdata = json_encode($data);
socket_write($socket, $encdata, strlen($encdata));
socket_close($socket);
echo 'Sent data\n';
?>
original source: github how to connect php to node js